Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Power BI taking ages to apply query changes

Hi all,

 

I have a data set that's similar to the following (but over 4000 rows)

 

Project    Data Category    Data Type    Source    Destination      Time Period    Amount    Index

A             Cash Flow           Baseline       Source1  DestinationA    Q1YYYY          #####      1

B             Expected Value   Actual          Source2  DestinationA    Q2YYYY         #####       2

C             Spend Down       Forecast      Source3  DestinationB    Q1YYYY          #####       3

 

The data that I have in the "Amount" Column is cumulative. I need to make it into specific data, individual for each time period. The code I have to do that is the following:

 

#"test" = Table.AddColumn(#"PreviousStep","Specific", 
    each if [Data Category] <> "Fund Budget" 
    then if [Funding Source] = #"PreviousStep"{[Index]-2}[Funding Source] and [Funding Destination] = #"PreviousStep"{[Index]-2}[Funding Destination] and [Project] = #"PreviousStep"{[Index]-2}[Project] and [Data Category] = #"PreviousStep"{[Index]-2}[Data Category] and [Data Type] = #"PreviousStep"{[Index]-2}[Data Type] 
    then [Amount] - #"PreviousStep"{[Index]-2}[Amount] 
    else if [Data Type] = "Forecast" 
    then [Amount] - Number.From(List.Last(List.RemoveNulls(List.Generate(()=>[x = 0, y = null], each [x] < List.Count(Table.Column(#"PreviousStep", "Amount")), each [x = [x] + 1, y = each if #"PreviousStep"{[x]}[Project] = [Project] and #"PreviousStep"{[x]}[Data Category] = [Data Category] and #"PreviousStep"{[x]}[Data Type] = "Actual" and #"PreviousStep"{[x]}[Funding Source] = [Funding Source] and #"PreviousStep"{[x]}[Funding Destination] =[Funding Destination] then #"PreviousStep"{[x]}[Amount] else null], each [y])))) 
    else [Amount] 
    else [Amount])

Basically, the code is supposed to work as follows. As the query creates a new column, it checks first to see the Data Category- anything that's part of a "Fund Budget" does NOT get unfolded into a specific number, it just stays in cumulative form. Next, it checks to see if the Data Type, Category, Source, and Destination match between the row in question and the previous row (all of the data is listed in chunks ordered by time period). If they do, the specific amount is calculated just by subtracting the cumulative amount from the previous time period from the cumulative amount of the current time period.

 

The next part is the tricky bit. For Forecasted data, the specific period amount for the first point (i.e. the data that doesn't match the immediate precious row) should be calculated by subtracting out the cumulative from the Actual data of the previous time period. My idea of how to do this involves using List.Generate to separate out the Actual data that matches the Data Category, Source, and Destination of each of the Forecast initial points and grabbing the last of that list to subtract from the Forecast amount. So far, the code seems to do what it's supposed to.

 

But when I click "Close & Apply" the model takes ages to actually update (as in 2 hours and counting). It didn't do this until I added the Forecast specific if-then-else statement, and I understand that adding a bit more time to the update and apply process (it's a pretty complex generation trick, after all) but 2 hours? I find that a little ridiculous.

 

So do you all see something that maybe I missed that's making this way more difficult than it needs to be? Is there a way to speed up this process? Unfortunately I am restricted to using power query for this- I thought about moving it into Python initially but due to restrictions from the top brass I have to do all of my transformations in Power BI.

 

Thanks,

Jason

4 REPLIES 4
v-juanli-msft
Community Support
Community Support

Hi @Anonymous 

Could you show me expected result ?

 

As for speed up performance, there are some reference:

Speeding up power query

Performance tip for List.Generate (1): Buffer your tables in Power BI and Power Query

 

Best Regards
Maggie

 

Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Stachu
Community Champion
Community Champion

The model you work on will be used with the PowerBI visuals, correct?

 

The logic you describe should be much easier to achieve in DAX, possibly even without storing cumulative values, but just with cumulative measures. Is that an option for you or is there a particular reason why you need to store the data in the table like that?

 



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Anonymous
Not applicable

Yes, everything will be in Power BI. I don't know DAX very well, though- how would I go about exploring it?

 

Thanks,

Jason

Stachu
Community Champion
Community Champion

it's quite a journey before you Smiley Wink

 

this topic has lots of sources:
https://community.powerbi.com/t5/Desktop/Good-Source-to-Learn-DAX/m-p/537748

 

here is an example for the cumulative totals:

https://www.daxpatterns.com/cumulative-total/

 

regarding your particular model it would be better to revisit once you learn the basic concepts, especially if you come from SQL background - DAX has very different way of solving same problems

if you're under time pressure though here are few tips:

1) instead of quarters as text I'd use dates (e.g. 2019-01-01 for 2019Q1, 2019-04-01 for 2019Q2). This enables using more operators, as well as gives access to the time intelligence functions

2) build a separate Calendar (date dimension) table (for the benefit of time intelligence):

https://www.sqlbi.com/articles/automatic-time-intelligence-in-power-bi/

https://docs.microsoft.com/en-us/power-bi/desktop-date-tables

3) if the baseline scenario is just prior year figures you could calculate these values based on Actual & Calendar table (e.g. using SAMEPERIODLASTYEAR)
https://docs.microsoft.com/en-us/dax/sameperiodlastyear-function-dax



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors