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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
PowerWhy
Helper III
Helper III

Sharing a temporary table between measures

Hello,

 

Please can you tell me if it's possible to calculate a temporary table in a measure, and then share that table with other measures (without it having to be calculated each time).

 

I have 8 columns that all need to calculate the same temporary table and it's taking 50 seconds to run, so I was thinking if the table can be shared that could reduce to about 6 seconds?

 

Thank you,

 

CW

1 ACCEPTED SOLUTION
speedramps
Super User
Super User

The answer is  No and Yes

 

No ... you can not crate a measure in one measure and use it in another. ☹️☹️

 

But ... yes ...  I have a solution to your  50 second problem 😀😀

 

Click here to download the solution 

 

How it works ...

 

First use Enter Data to manaully create a driver table with the names of your 8 meaures

For example her are 6 ...

speedramps_1-1666533227737.png

 

Then create a DAX measure which uses VAR to build your temp table once.
Note it is much faster to aggergate using ADDCOLUMNS rather that SUMMARIZE

Then use SWITCH to reuse the temp table 8 times.

 
Dynamic =
VAR salesbycountry = ADDCOLUMNS(VALUES(Customers[Country]),"CSales",[Total Sales],"CCosts",[Total Costs],"CTax",[Total Tax])

RETURN

SWITCH(SELECTEDVALUE(Driver[ID]),
1, SUMX(salesbycountry,[CSales]),
2, SUMX(salesbycountry,[CSales] + [CTax]),
3, SUMX(salesbycountry,[CSales] - [CCosts])
)

 

For example this DAX meassure creates salesbycountry temp table.

Then uses salesbycountry to create lossbycountry and profitbycountry temp tables.

Then uses SWITCH to create 6 measure.

This method can easily reduce 90 seconds masures to under 5 seconds.

Please let me know how you get on.

 

Filtered =
VAR salesbycountryADDCOLUMNS(VALUES(Customers[Country]),"CSales",[Total Sales],"CCosts",[Total Costs],"CTax",[Total Tax])
VAR lossbycountry = FILTER(salesbycountry,[CCosts] > [CSales])
VAR profitbycountry = FILTER(salesbycountry,[CSales] >= [CCosts])

RETURN

SWITCH(SELECTEDVALUE(Driver[ID]),
1, SUMX(lossbycountry,[CSales]),
2, SUMX(lossbycountry,[CSales] + [CTax]),
3, SUMX(lossbycountry,[CSales] - [CCosts]),
4, SUMX(profitbycountry,[CSales]),
5, SUMX(profitbycountry,[CSales] + [CTax]),
6, SUMX(profitbycountry,[CSales] - [CCosts])
)
 
 
speedramps_0-1666534191214.png

 

 
 
 

Thanks for reaching out for help.

I put in a lot of effort to help you, now please quickly help me by giving kudos.

Remember we are unpaid volunteers and here to coach you with Power BI and DAX skills and techniques, not do the users job for them. So please click the thumbs up and accept as solution button. 

If you give someone a fish then you only give them one meal, but if you teach them how to fish then they can feed themselves and teach others for a lifetime.  I prefer to teach members on this forum techniques rather give full solutions and do their job. You can then adapt the technique for your solution, learn some DAX skills for next time and soon become a Power BI Super User like me. 

One question per ticket please. If you need to extend your request then please raise a new ticket.

You will get a quicker response and each volunteer solver will get the kudos they deserve. Thank you ! 

 

 

 

 

 

 

 

View solution in original post

2 REPLIES 2
speedramps
Super User
Super User

The answer is  No and Yes

 

No ... you can not crate a measure in one measure and use it in another. ☹️☹️

 

But ... yes ...  I have a solution to your  50 second problem 😀😀

 

Click here to download the solution 

 

How it works ...

 

First use Enter Data to manaully create a driver table with the names of your 8 meaures

For example her are 6 ...

speedramps_1-1666533227737.png

 

Then create a DAX measure which uses VAR to build your temp table once.
Note it is much faster to aggergate using ADDCOLUMNS rather that SUMMARIZE

Then use SWITCH to reuse the temp table 8 times.

 
Dynamic =
VAR salesbycountry = ADDCOLUMNS(VALUES(Customers[Country]),"CSales",[Total Sales],"CCosts",[Total Costs],"CTax",[Total Tax])

RETURN

SWITCH(SELECTEDVALUE(Driver[ID]),
1, SUMX(salesbycountry,[CSales]),
2, SUMX(salesbycountry,[CSales] + [CTax]),
3, SUMX(salesbycountry,[CSales] - [CCosts])
)

 

For example this DAX meassure creates salesbycountry temp table.

Then uses salesbycountry to create lossbycountry and profitbycountry temp tables.

Then uses SWITCH to create 6 measure.

This method can easily reduce 90 seconds masures to under 5 seconds.

Please let me know how you get on.

 

Filtered =
VAR salesbycountryADDCOLUMNS(VALUES(Customers[Country]),"CSales",[Total Sales],"CCosts",[Total Costs],"CTax",[Total Tax])
VAR lossbycountry = FILTER(salesbycountry,[CCosts] > [CSales])
VAR profitbycountry = FILTER(salesbycountry,[CSales] >= [CCosts])

RETURN

SWITCH(SELECTEDVALUE(Driver[ID]),
1, SUMX(lossbycountry,[CSales]),
2, SUMX(lossbycountry,[CSales] + [CTax]),
3, SUMX(lossbycountry,[CSales] - [CCosts]),
4, SUMX(profitbycountry,[CSales]),
5, SUMX(profitbycountry,[CSales] + [CTax]),
6, SUMX(profitbycountry,[CSales] - [CCosts])
)
 
 
speedramps_0-1666534191214.png

 

 
 
 

Thanks for reaching out for help.

I put in a lot of effort to help you, now please quickly help me by giving kudos.

Remember we are unpaid volunteers and here to coach you with Power BI and DAX skills and techniques, not do the users job for them. So please click the thumbs up and accept as solution button. 

If you give someone a fish then you only give them one meal, but if you teach them how to fish then they can feed themselves and teach others for a lifetime.  I prefer to teach members on this forum techniques rather give full solutions and do their job. You can then adapt the technique for your solution, learn some DAX skills for next time and soon become a Power BI Super User like me. 

One question per ticket please. If you need to extend your request then please raise a new ticket.

You will get a quicker response and each volunteer solver will get the kudos they deserve. Thank you ! 

 

 

 

 

 

 

 

Greg_Deckler
Super User
Super User

@PowerWhy Unfortunately measures cannot return tables. I would love that feature. You might be able to increase performance using "horizontal fusion" preview feature in October release. Yet another super useful but forgotten idea:

Search (powerbi.com)

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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