Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

include only same values that are in previous month

Hello, Im strugling with calculation that should include only same values that are in previous month. I need to calculate % change of price over months, but I have to include only values that are f...
  • Icey's avatar
    5 years ago

    Hi Anonymous ,

     

    Try this:

    1. Create a "StartDayofMonth" column in your Fact table.

    StartDayofMonth =
    CONVERT ( [month] & "/1", DATETIME )
    

     

    2. Create another Dates table.

    Dates =
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2020, 4, 30 ) ),
        "YearMonth", FORMAT ( [Date], "YYYY/MM" )
    )
    

     

    3. Create another Types table.

    Types =
    DISTINCT ( 'Fact Table'[type] )
    

     

    4. Create relationships.

     

    5. Create measures.

    ModifiedCurrentMonthPrice = 
    VAR CurrentMonthPrice =
        SUM ( 'Fact Table'[price] )
    VAR PreviousMonthPrice =
        CALCULATE ( SUM ( 'Fact Table'[price] ), PREVIOUSMONTH ( Dates[Date] ) )
    VAR ModifiedCurrentMonthPrice =
        IF ( PreviousMonthPrice = BLANK (), BLANK (), CurrentMonthPrice )
    RETURN
        ModifiedCurrentMonthPrice
    
    ModifiedPreviousMonthPrice = 
    VAR CurrentMonthPrice =
        SUM ( 'Fact Table'[price] )
    VAR PreviousMonthPrice =
        CALCULATE ( SUM ( 'Fact Table'[price] ), PREVIOUSMONTH ( Dates[Date] ) )
    VAR ModifiedPreviousMonthPrice =
        IF ( CurrentMonthPrice = BLANK (), BLANK (), PreviousMonthPrice )
    RETURN
        ModifiedPreviousMonthPrice
    
    % MoM change = 
    VAR SumCurrent =
        SUMX ( Types, [ModifiedCurrentMonthPrice] )
    VAR SumPrevious =
        SUMX ( Types, [ModifiedPreviousMonthPrice] )
    RETURN
        DIVIDE ( SumCurrent - SumPrevious, SumPrevious )
    

    BTW, .pbix file attached.

     

     

    Best regards

    Icey

     

    If this post helps, then consider Accepting it as the solution to help other members find it faster.