I will like a script in power bi that will calculate percentage increase or decrease from one month to the previous month

拈花ヽ惹草 提交于 2019-12-11 15:13:26

问题


I want to display percentage increase or decrease in total for each month as I select each month i.e when I click on FEB, it should tell me whether there was a percentage increase/decrease in expenses compared to JAN.

I have tried different codes but keep getting an error message.

Here is a DAX CODE I tried:

change perc =
VAR ValueLastMONTH =
CALCULATE (
    SUM ( population[TOTAL] ),
    FILTER (
        population,
        population[MONTH]
            = ( EARLIER ( population[MONTH] ) - 1 )
            && population[CATEGORY] = EARLIER ( population[CATEGORY] )

    )
)
RETURN
IF (
    ISBLANK ( ValueLastMONTH ),
    0,
    ( population[TOTAL] - ValueLastMONTH )
        / ValueLastMONTH

I want a new column created to display the percentage increase or decrease from a month to its previous month. Here is a screenshot of the excel document:


回答1:


The Column 'Month' is not of type date. How would PowerBi know the text APR represents April? You need to make this column a date.

Now you need to change the script to work with DateDiff:

change perc = 
VAR ValueLastMONTH =
    CALCULATE (
        SUM ( population[TOTAL] ),
        FILTER (
            population,
            DATEDIFF(population[MONTH], EARLIER ( population[MONTH] ),MONTH) = 1
                && population[CATEGORY] = EARLIER ( population[CATEGORY] )

        )
    )
RETURN
    IF (
        ISBLANK ( ValueLastMONTH );
        0; 
        ( population[TOTAL] - ValueLastMONTH )
            / ValueLastMONTH)


来源:https://stackoverflow.com/questions/56993083/i-will-like-a-script-in-power-bi-that-will-calculate-percentage-increase-or-decr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!