Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
Hello all, i need help wrt to this post of myself. I got a solution from @Anonymous (thanks for that). But unfortunately this is working only i have a context. The current solution is working for matrix & table views only. If i apply the calculation on Bar Chart or any other visuals its going null/blank.
https://community.fabric.microsoft.com/t5/Desktop/Difference-in-Meter-Reading-Calculation-based-on-dates/m-p/4237950#M1332051
How can i modify the current calculation to work for visuals.
Difference =
VAR _shifttype =
SELECTEDVALUE ( 'Table'[ShiftStartOrEnd] )
VAR _date =
SELECTEDVALUE ( 'Table'[ReadingCaptureOn] )
VAR _shift =
SELECTEDVALUE ( 'Table'[Shift Name] )
VAR _input =
SELECTEDVALUE ( 'Table'[Input] )
VAR _ss1 =
CALCULATE (
SUM ( 'Table'[Value] ),
FILTER ( 'Table', 'Table'[Shift Name] = "Shift 1"&&'Table'[Input]=_input&&'Table'[ReadingCaptureOn]=_date )
)
VAR _ss2 =
CALCULATE (
SUM ( 'Table'[Value] ),
FILTER ( ALLSELECTED('Table'), 'Table'[Shift Name] = "Shift 2"&&'Table'[Input]=_input&&'Table'[ReadingCaptureOn]=_date )
)
VAR _ss3 =
CALCULATE (
SUM ( 'Table'[Value] ),
FILTER ( ALLSELECTED('Table'), 'Table'[Shift Name] = "Shift 3" &&'Table'[Input]=_input&&'Table'[ReadingCaptureOn]=_date )
)
VAR _es1 =
CALCULATE (
SUM ( 'Table'[Value] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[Shift Name] = "Shift 1"
&& 'Table'[Input] = _input
&& 'Table'[ReadingCaptureOn] = _date + 1
)
)
VAR _es2 =
CALCULATE (
SUM ( 'Table'[Value] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[Shift Name] = "Shift 2"
&& 'Table'[Input] = _input
&& 'Table'[ReadingCaptureOn] = _date + 1
)
)
VAR _es3 =
CALCULATE (
SUM ( 'Table'[Value] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[Shift Name] = "Shift 3"
&& 'Table'[Input] = _input
&& 'Table'[ReadingCaptureOn] = _date + 1
)
)
RETURN
SWITCH (
TRUE (),
_shifttype = "Start"
&& _shift = "Shift 1", _ss1 - _ss2,
_shifttype = "Start"
&& _shift = "Shift 2", _ss2 - _ss3,
_shifttype = "Start"
&& _shift = "Shift 3", _ss3 - _es1,
_shifttype = "End"
&& _shift = "Shift 1", _ss3 - _es1,
_shifttype = "End"
&& _shift = "Shift 2", _es1 - _es2,
_shifttype = "End"
&& _shift = "Shift 3", _es2 - _es3
)@amitchandak @Greg_Deckler @Anonymous
Try the below measure
Difference =
VAR _shifttype = SELECTEDVALUE('Table'[ShiftStartOrEnd])
VAR _date = SELECTEDVALUE('Table'[ReadingCaptureOn])
VAR _shift = SELECTEDVALUE('Table'[Shift Name])
VAR _input = SELECTEDVALUE('Table'[Input])
VAR _ss1 =
CALCULATE(
SUMX(
VALUES('Table'[ReadingCaptureOn]),
CALCULATE(SUM('Table'[Value]), 'Table'[Shift Name] = "Shift 1" && 'Table'[Input] = _input)
),
'Table'[ReadingCaptureOn] = _date
)
VAR _ss2 =
CALCULATE(
SUMX(
VALUES('Table'[ReadingCaptureOn]),
CALCULATE(SUM('Table'[Value]), 'Table'[Shift Name] = "Shift 2" && 'Table'[Input] = _input)
),
'Table'[ReadingCaptureOn] = _date
)
VAR _ss3 =
CALCULATE(
SUMX(
VALUES('Table'[ReadingCaptureOn]),
CALCULATE(SUM('Table'[Value]), 'Table'[Shift Name] = "Shift 3" && 'Table'[Input] = _input)
),
'Table'[ReadingCaptureOn] = _date
)
VAR _es1 =
CALCULATE(
SUMX(
VALUES('Table'[ReadingCaptureOn]),
CALCULATE(SUM('Table'[Value]), 'Table'[Shift Name] = "Shift 1" && 'Table'[Input] = _input)
),
'Table'[ReadingCaptureOn] = _date + 1
)
VAR _es2 =
CALCULATE(
SUMX(
VALUES('Table'[ReadingCaptureOn]),
CALCULATE(SUM('Table'[Value]), 'Table'[Shift Name] = "Shift 2" && 'Table'[Input] = _input)
),
'Table'[ReadingCaptureOn] = _date + 1
)
VAR _es3 =
CALCULATE(
SUMX(
VALUES('Table'[ReadingCaptureOn]),
CALCULATE(SUM('Table'[Value]), 'Table'[Shift Name] = "Shift 3" && 'Table'[Input] = _input)
),
'Table'[ReadingCaptureOn] = _date + 1
)
RETURN
SWITCH(
TRUE(),
_shifttype = "Start" && _shift = "Shift 1", _ss1 - _ss2,
_shifttype = "Start" && _shift = "Shift 2", _ss2 - _ss3,
_shifttype = "Start" && _shift = "Shift 3", _ss3 - _es1,
_shifttype = "End" && _shift = "Shift 1", _ss3 - _es1,
_shifttype = "End" && _shift = "Shift 2", _es1 - _es2,
_shifttype = "End" && _shift = "Shift 3", _es2 - _es3
)
Best Regards
Saud Ansari
If this post helps, please Accept it as a Solution to help other members find it. I appreciate your Kudos!
Hi @saud968 thanks for the solution, but it doesnt work
Before
After
When i remove Shift it should aggregate or should display totals but its going blank.
Here’s an updated version of your DAX formula that includes a check for when the ‘Shift’ context is not present:
Difference =
VAR _shifttype = SELECTEDVALUE('Table'[ShiftStartOrEnd])
VAR _date = SELECTEDVALUE('Table'[ReadingCaptureOn])
VAR _input = SELECTEDVALUE('Table'[Input])
VAR _shift = SELECTEDVALUE('Table'[Shift Name], "All Shifts")
VAR _ss1 =
CALCULATE(
SUM('Table'[Value]),
'Table'[Shift Name] = "Shift 1" && 'Table'[Input] = _input && 'Table'[ReadingCaptureOn] = _date
)
VAR _ss2 =
CALCULATE(
SUM('Table'[Value]),
'Table'[Shift Name] = "Shift 2" && 'Table'[Input] = _input && 'Table'[ReadingCaptureOn] = _date
)
VAR _ss3 =
CALCULATE(
SUM('Table'[Value]),
'Table'[Shift Name] = "Shift 3" && 'Table'[Input] = _input && 'Table'[ReadingCaptureOn] = _date
)
VAR _es1 =
CALCULATE(
SUM('Table'[Value]),
'Table'[Shift Name] = "Shift 1" && 'Table'[Input] = _input && 'Table'[ReadingCaptureOn] = _date + 1
)
VAR _es2 =
CALCULATE(
SUM('Table'[Value]),
'Table'[Shift Name] = "Shift 2" && 'Table'[Input] = _input && 'Table'[ReadingCaptureOn] = _date + 1
)
VAR _es3 =
CALCULATE(
SUM('Table'[Value]),
'Table'[Shift Name] = "Shift 3" && 'Table'[Input] = _input && 'Table'[ReadingCaptureOn] = _date + 1
)
RETURN
IF(
_shift = "All Shifts",
SUMX(
VALUES('Table'[Shift Name]),
SWITCH(
TRUE(),
_shifttype = "Start", _ss1 - _ss2 + _ss2 - _ss3 + _ss3 - _es1,
_shifttype = "End", _ss3 - _es1 + _es1 - _es2 + _es2 - _es3
)
),
SWITCH(
TRUE(),
_shifttype = "Start" && _shift = "Shift 1", _ss1 - _ss2,
_shifttype = "Start" && _shift = "Shift 2", _ss2 - _ss3,
_shifttype = "Start" && _shift = "Shift 3", _ss3 - _es1,
_shifttype = "End" && _shift = "Shift 1", _ss3 - _es1,
_shifttype = "End" && _shift = "Shift 2", _es1 - _es2,
_shifttype = "End" && _shift = "Shift 3", _es2 - _es3
)
)
This formula includes a check for when the ‘Shift’ context is not present (_shift = "All Shifts"), and it aggregates the values accordingly.
Best Regards
Saud Ansari
If this post helps, please Accept it as a Solution to help other members find it. I appreciate your Kudos!
HI @saud968 thanks for providing the solution. Unfortunately the totals are not correct. And it is not just the shift i need i might need for Shiftinput also. How can we make different approach for this. Please guide me.
Hopefully this will workout for you
Difference =
VAR SummaryTable =
SUMMARIZE(
'Table',
'Table'[ReadingCaptureOn],
'Table'[Shift Name],
'Table'[ShiftStartOrEnd],
'Table'[Input],
"TotalValue", SUM('Table'[Value])
)
VAR _shifttype = SELECTEDVALUE('Table'[ShiftStartOrEnd])
VAR _date = SELECTEDVALUE('Table'[ReadingCaptureOn])
VAR _input = SELECTEDVALUE('Table'[Input])
VAR _shift = SELECTEDVALUE('Table'[Shift Name], "All Shifts")
VAR _ss1 =
CALCULATE(
SUMX(
FILTER(SummaryTable, [Shift Name] = "Shift 1" && [Input] = _input && [ReadingCaptureOn] = _date),
[TotalValue]
)
)
VAR _ss2 =
CALCULATE(
SUMX(
FILTER(SummaryTable, [Shift Name] = "Shift 2" && [Input] = _input && [ReadingCaptureOn] = _date),
[TotalValue]
)
)
VAR _ss3 =
CALCULATE(
SUMX(
FILTER(SummaryTable, [Shift Name] = "Shift 3" && [Input] = _input && [ReadingCaptureOn] = _date),
[TotalValue]
)
)
VAR _es1 =
CALCULATE(
SUMX(
FILTER(SummaryTable, [Shift Name] = "Shift 1" && [Input] = _input && [ReadingCaptureOn] = _date + 1),
[TotalValue]
)
)
VAR _es2 =
CALCULATE(
SUMX(
FILTER(SummaryTable, [Shift Name] = "Shift 2" && [Input] = _input && [ReadingCaptureOn] = _date + 1),
[TotalValue]
)
)
VAR _es3 =
CALCULATE(
SUMX(
FILTER(SummaryTable, [Shift Name] = "Shift 3" && [Input] = _input && [ReadingCaptureOn] = _date + 1),
[TotalValue]
)
)
RETURN
IF(
_shift = "All Shifts",
SUMX(
VALUES('Table'[Shift Name]),
SWITCH(
TRUE(),
_shifttype = "Start", _ss1 - _ss2 + _ss2 - _ss3 + _ss3 - _es1,
_shifttype = "End", _ss3 - _es1 + _es1 - _es2 + _es2 - _es3
)
),
SWITCH(
TRUE(),
_shifttype = "Start" && _shift = "Shift 1", _ss1 - _ss2,
_shifttype = "Start" && _shift = "Shift 2", _ss2 - _ss3,
_shifttype = "Start" && _shift = "Shift 3", _ss3 - _es1,
_shifttype = "End" && _shift = "Shift 1", _ss3 - _es1,
_shifttype = "End" && _shift = "Shift 2", _es1 - _es2,
_shifttype = "End" && _shift = "Shift 3", _es2 - _es3
)
)
Best Regards
Saud Ansari
If this post helps, please Accept it as a Solution to help other members find it. I appreciate your Kudos!
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 50 | |
| 44 | |
| 42 | |
| 19 | |
| 19 |
| User | Count |
|---|---|
| 70 | |
| 68 | |
| 33 | |
| 32 | |
| 32 |