Forum Discussion

PowerWhy's avatar
PowerWhy
Helper IV
3 years ago
Solved

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

  • 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 ...

     

    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])
    )
     
     

     

     
     
     

    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 ! 

     

     

     

     

     

     

     

2 Replies

  • 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 ...

     

    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])
    )
     
     

     

     
     
     

    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's avatar
    Greg_Deckler
    Community Champion

    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)