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
halskwibskc
Frequent Visitor

calcultate previous week according to the row selection dynamically in matrix

hi all,

 

what i want to achieve is that in Matrix, it could calculate previous week amount smartly according to the field that i put in row section: e.g. 

when i put country in Rows, it shows

halskwibskc_0-1664202483547.png

 

and if i put product in Rows, it will dynamtically change to

halskwibskc_1-1664202503792.png

 

I am able to get previous week amount by the following DAX, however the problem is, because i sepecify Country in the DAX, if i want to calculate product, I would need to create another measure for it. What I want is just 1 measure to achieve this dynamic change, is it possible? 

 

PreviousWeekPaidImpr =
var currentweeknum = SELECTEDVALUE(bq[weeknum cloumn])
return
CALCULATE([Paid Impr],FILTER(ALLEXCEPT(bq,bq[country]),bq[weeknum cloumn]= currentweeknum -1))
 
 
 
FYI, I tried 
 
PreviousWeekPaidImpr =
var currentweeknum = SELECTEDVALUE(bq[weeknum cloumn])
return
CALCULATE([Paid Impr],FILTER(ALL(bq),bq[weeknum cloumn]= currentweeknum -1))
 
but with this, it returns result: which is not what I want. 
halskwibskc_2-1664202532302.png

 

5 REPLIES 5
Greg_Deckler
Super User
Super User

@halskwibskc Maybe:

PreviousWeekPaidImpr =
var currentweeknum = SELECTEDVALUE(bq[weeknum cloumn])
var country = CALCULATE([Paid Impr],FILTER(ALLEXCEPT(bq,bq[country]),bq[weeknum cloumn]= currentweeknum -1))
var product = CALCULATE([Paid Impr],FILTER(ALLEXCEPT(bq,bq[product]),bq[weeknum cloumn]= currentweeknum -1))
RETURN
  IF(ISINSCOPE('bq'[country]),country,product)

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

hi @Greg_Deckler thanks for the quick reply.

 

there are acutally more than 2 fields that i want to change in the Rows section. Apart from country and products which are already mentioned, there are also sizes, colours and clients etc. 
In this case, the IF function would not be able to cover all the scenario. 

Do you have a new solution for it? 

@halskwibskc You could do a nested IF statement but a SWITCH TRUE would be better, like below. Also, are you using a Field Parameter or no?

PreviousWeekPaidImpr =
var currentweeknum = SELECTEDVALUE(bq[weeknum cloumn])
var country = CALCULATE([Paid Impr],FILTER(ALLEXCEPT(bq,bq[country]),bq[weeknum cloumn]= currentweeknum -1))
var product = CALCULATE([Paid Impr],FILTER(ALLEXCEPT(bq,bq[product]),bq[weeknum cloumn]= currentweeknum -1))
RETURN
  SWITCH(TRUE(),
    ISINSCOPE('bq'[country]),country,
    ISINSCOPE('bq'[product]),product,
    // more ISINSCOPE here
  )

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

hi @Greg_Deckler  thanks for the reply! I tired the swtich to calcaulate the difference between this week and previous week. However, the issue with swtich is that the sum total of the difference is missing if i use swtich. Do you have a way to work around this? I need the total number for futhere calculation steps. 

halskwibskc_0-1664361943656.png

 

hi @Greg_Deckler  in case you missed this, I'm tagging here again.  it's highly appreciated if you have a solution for this, thanks!

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