I'm going to make in PowerBI chart with monthly differences in sales. I have data as below - sales and two types of date:
DATE_YYYYMM | DATE_MMMYY | SALES |
---|---|---|
202401 | Jan'24 | 100 |
202401 | Jan'24 | 110 |
202402 | Feb'24 | 110 |
202402 | Feb'24 | 120 |
202403 | Mar'24 | 120 |
202403 | Mar'24 | 130 |
202404 | Apr'24 | 130 |
202404 | Apr'24 | 140 |
I wrote measure to count sales differences between each month:
MONTHLY_SALES_VS_PREVIOUS_MONTH =
VAR _SELECTEDVALUE = SELECTEDVALUE('Table'[DATE_YYYYMM])
VAR _MAX_DATE = CALCULATE(MAX('Table'[DATE_YYYYMM]), ALL('Table'[DATE_YYYYMM]), FILTER(ALL('Table'), 'Table'[DATE_YYYYMM] < _SELECTEDVALUE))
VAR PREVIOUS_PERFORMANCE = CALCULATE(SUM('Table'[SALES]), 'Table'[DATE_YYYYMM] = _MAX_DATE)
VAR CURRENT_PERFORMANCE = SUM('Table'[SALES])
RETURN CURRENT_PERFORMANCE / PREVIOUS_PERFORMANCE - 1
When I use this measure to make a chart in PowerBI, I have something like this:
It is correct, but I want to have chart with other type of date on X-axis. Like this:
I tried to modify my measure, but it don't want to work with this second type of date. Is there any way to reach this result?
You need a proper date table and star schema but if you want a quick hack, try this.
MONTHLY_SALES_VS_PREVIOUS_MONTH =
VAR _SELECTEDVALUE = SELECTEDVALUE('Table'[DATE_YYYYMM])
VAR _MAX_DATE = CALCULATE(MAX('Table'[DATE_YYYYMM]), ALL('Table'[DATE_YYYYMM]), FILTER(ALL('Table'), 'Table'[DATE_YYYYMM] < _SELECTEDVALUE))
VAR PREVIOUS_PERFORMANCE = CALCULATE(SUM('Table'[SALES]), 'Table'[DATE_YYYYMM] = _MAX_DATE, REMOVEFILTERS())
VAR CURRENT_PERFORMANCE = SUM('Table'[SALES])
RETURN IF(_SELECTEDVALUE <> 202401, CURRENT_PERFORMANCE / PREVIOUS_PERFORMANCE -1)