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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

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
Community Champion
Community Champion

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


Follow on LinkedIn
@ 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!:
DAX For Humans

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
  )


Follow on LinkedIn
@ 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!:
DAX For Humans

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors