Nesting This Function

schwiggiddy

New Member
Joined
Sep 5, 2017
Messages
31
I'm attempting to have PBI do some linear regression for me. From a helper query that iterates over multiple files, I'm calling this custom function to determine the highest reading above and lowest reading below the linear regression line over a variable length period for each row of data. It's working, but I'm getting an error on the service that says it cannot schedule a refresh because of (I think) the source of this query/function (it says Query1; I called this fnQuery1. The error is about a dynamic data source; this seems dynamic).

How would/could I go about integrating this function back into the helper query's main line of operations so as to eliminate the cause for the error? Two of the variables being passed to it are lists (reading and antiindex) and the other two are decimal numbers.

Have I overcomplicated the matter? Is there an easier way to do this that I've overlooked? Any help in the right direction would be most appreciated.

Power Query:
(reading as any, slopeb, intercepta, antiindex) =>
let
// Convert single values to single entry lists
    listslopeb = {slopeb},  
    listintercepta = {intercepta},

// Combine 4 lists into a 4 column table
    Source = Table.FromColumns({reading,listslopeb,listintercepta,antiindex},{"Reading", "Slopeb", "Intercepta", "AntiIndex"}), 

// Fill the single value columns down to all rows
    FillDown = Table.FillDown(Source, {"Slopeb", "Intercepta"}),

// Calculate the position of the linear regression line at each antiindex value
    AddY = Table.AddColumn(FillDown, "YValue", each [Intercepta] + [AntiIndex] * [Slopeb]),

// Determine highest and lowest values from the linear regression line 
    SubtractReading = Table.AddColumn(AddY, "Delta", each [Reading] - [YValue]),
    MaxDelta = Table.AddColumn(SubtractReading, "Max", each List.Max(SubtractReading[Delta])),
    MinDelta = Table.AddColumn(MaxDelta, "Min", each List.Min(SubtractReading[Delta])),

// Return just a single row of data since the max and min are the same for each row
    Return = Table.FirstN(MinDelta, 1)
in
    Return
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I'm not sure about what you are asking, but if you are asking how to incorporate a custom function into a query, then here's an example:

Here's an example of an independent function that multiplies each number in a row of a table by a multiplier:

Power Query:
(Source as table, rw as number, multiplier as number) as table =>
let
    ttr = Table.ToRows(Source),
    ltt = List.Transform(ttr{rw},each _*multiplier),
    lstNew = List.FirstN(ttr,rw) & {ltt} & List.Skip(ttr,rw+1),
    tfr = Table.FromRows(lstNew,Table.ColumnNames(Source))
in
    tfr

I can incorporate it into another query like this:

Power Query:
let
    Row_Mult = (Source as table, rw as number, multiplier as number) as table =>
        let
            ttr = Table.ToRows(Source),
            ltt = List.Transform(ttr{rw},each _*multiplier),
            lstNew = List.FirstN(ttr,rw) & {ltt} & List.Skip(ttr,rw+1),
            tfr = Table.FromRows(lstNew,Table.ColumnNames(Source))
        in
            tfr,
    Tbl = Table.FromRecords({
                                [Col1 = 1, Col2 =2, Col3 = 3],
                                [Col1 = 4, Col2 =5, Col3 = 6],
                                [Col1 = 7, Col2 =8, Col3 = 9]}),
    Result = Row_Mult(Tbl,1,.01)
in
    Result
 
Upvote 0
Solution
I'm not sure about what you are asking, but if you are asking how to incorporate a custom function into a query, then here's an example:

Here's an example of an independent function that multiplies each number in a row of a table by a multiplier:

Power Query:
(Source as table, rw as number, multiplier as number) as table =>
let
    ttr = Table.ToRows(Source),
    ltt = List.Transform(ttr{rw},each _*multiplier),
    lstNew = List.FirstN(ttr,rw) & {ltt} & List.Skip(ttr,rw+1),
    tfr = Table.FromRows(lstNew,Table.ColumnNames(Source))
in
    tfr

I can incorporate it into another query like this:

Power Query:
let
    Row_Mult = (Source as table, rw as number, multiplier as number) as table =>
        let
            ttr = Table.ToRows(Source),
            ltt = List.Transform(ttr{rw},each _*multiplier),
            lstNew = List.FirstN(ttr,rw) & {ltt} & List.Skip(ttr,rw+1),
            tfr = Table.FromRows(lstNew,Table.ColumnNames(Source))
        in
            tfr,
    Tbl = Table.FromRecords({
                                [Col1 = 1, Col2 =2, Col3 = 3],
                                [Col1 = 4, Col2 =5, Col3 = 6],
                                [Col1 = 7, Col2 =8, Col3 = 9]}),
    Result = Row_Mult(Tbl,1,.01)
in
    Result

YES! This helped me move forward. Thank you.

Unfortunately, the PBI service still doesn't like the data source for Query1 to enable scheduled refresh. I had thought the issue was in the function, but no such luck, as I've deleted the external function and still get that error. The search continues.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top