Power BI – Compare Measures over Period

I solved my calculation and found a workaround for the wrongly displayed date hierarchy. Sometimes the answer to a question if you’re not familiar with a programming language yet is simpler as one could expect.

Finished working code:

Market Share Prev Period = 
IF(
CALCULATE(
    SUM('Monthly Reports'[Sales]),
    PREVIOUSMONTH('Monthly Reports'[SaleDate])
) 
<> BLANK()
,
(
    DIVIDE(
        CALCULATE(
            SUM('Monthly Reports'[Sales])
        ), 
        CALCULATE(
            SUM('Monthly Reports'[Sales]),
            ALL('Monthly Reports'[Company])
        )
        ,
        0
    )
)
-
(
    DIVIDE(
        CALCULATE(
            SUM('Monthly Reports'[Sales]),
            PREVIOUSMONTH('Monthly Reports'[SaleDate])
        ), 
        CALCULATE(
            SUM('Monthly Reports'[Sales]),
            PREVIOUSMONTH('Monthly Reports'[SaleDate]),
            ALL('Monthly Reports'[Company])
        )
        ,
        0
    )
  ),
"-"
)

I figured out, that be using DateAdd(.., -1, Month) I only changed what was read, not in which column it goes. So I was always missing the most recent value, as off course it was not read by the -1 operation and I was missing the oldest report as there was none before off course. Basically I found the function: PREVIOUSMONTH (too simple to be true).

Furthermore, I added an IF clause before to check if I’m at the first column. If so, I’m dashing the value.

My only problem, which was left in the end, was that when displaying the date hierarchy I only saw dashes in all the fields of the matrix. However, if I displayed the whole date I could see all correct values. My workaround was to format the date with “mmmm”. This displayed the same. I hope I will not need the date in any further step, as I will need to come back to this again.

So my result looked exactly perfect. enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top