Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
I want Previous (Year/Quarter/Month/Day) Amounts for Current (Year/Quarter/Month/Day) Amounts
So I created separate Measures and put them in a Switch function based on selection or Year/Quarter/Month/Day.
YY = CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,YEAR))
QQ = CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,QUARTER))
MM = CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,MONTH))
DD = CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,DAY))
Previous =
VAR Selected = SWITCH(TRUE(),
ISFILTERED(Data[OrderDate].[Year]),"YEAR",
ISFILTERED(Data[OrderDate].[Quarter]),"QUARTER",
ISFILTERED(Data[OrderDate].[Month]),"MONTH",
ISFILTERED(Data[OrderDate].[Day]),"DAY","Nothing")
RETURN
SWITCH(TRUE(),
Selected="Year",[YY],
Selected="Quarter",[QQ],
Selected="Month",[MM],
Selected="Day",[DD],
"")
What I want is to put these separate calculation Measures inside the Switch function rather then calling them from outside, like
Previous =
VAR Selected = SWITCH(TRUE(),
ISFILTERED(Data[OrderDate].[Year]),"YEAR",
ISFILTERED(Data[OrderDate].[Quarter]),"QUARTER",
ISFILTERED(Data[OrderDate].[Month]),"MONTH",
ISFILTERED(Data[OrderDate].[Day]),"DAY","Nothing")
RETURN
SWITCH(TRUE(),
Selected="Year",CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,YEAR)),
Selected="Quarter",CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,QUARTER)),
Selected="Month",CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,MONTH)),
Selected="Day",CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,DAY)),
"")
But Calculate comes after RETURN and I want them inside the Switch function so
RETURN
SWITCH ..... (CALCULATE.....
RETURN
CALCULATE
It seems that you may just use one SWITCH Function.
I put them too in Variables which I tried earlier too but it worked this time and not that time.
Still, open for Suggestions...
Previous =
VAR Selected = SWITCH(TRUE(),
ISFILTERED(Data[OrderDate].[Year]),"YEAR",
ISFILTERED(Data[OrderDate].[Quarter]),"QUARTER",
ISFILTERED(Data[OrderDate].[Month]),"MONTH",
ISFILTERED(Data[OrderDate].[Day]),"DAY","Nothing")
VAR YY = CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,YEAR))
VAR QQ = CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,QUARTER))
VAR MM = CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,MONTH))
VAR DD = CALCULATE(SUM(Data[Total]),DATEADD(Data[OrderDate].[Date],-1,DAY))
RETURN
SWITCH(TRUE(),Selected="Year",YY,Selected="Quarter",QQ,Selected="Month",MM,Selected="Day",DD,"")
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
58 | |
56 | |
56 | |
38 | |
29 |
User | Count |
---|---|
75 | |
62 | |
45 | |
40 | |
39 |