Forum Discussion

DataVitalizer's avatar
DataVitalizer
Icon for Super User rankSuper User
5 years ago
Solved

Calculate OR SUMX alternatives in Language M

Hi Community,

Is there any alterantive way to rewrite the below formula in Language M:

CALCULATE(SUM[CA],PREVIOUSMONTH([Date]))

Thank you in advance. 

  • Hello DataVitalizer 

     

    You can filter you table with Table.SelectRows and then sum the CA-column

        SumPreviousMonth = List.Sum(Table.SelectRows(PreviousStep, each Date.IsInPreviousNMonths(_[Date], 1))[CA])

    Here a complete code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TczJDcAwCETRXjhbiCW2SS3I/bdhkILC9Y3+uMOEASJILwrBGQ5PABuSFWjCbiAJqwEnUPuYX7ILVoIiaYEFxPwnTClWci4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CA = _t, Date = _t]),
        PreviousStep = Table.TransformColumnTypes(Source,{{"CA", Int64.Type}, {"Date", type date, "de-DE"}}),
        SumPreviousMonth = List.Sum(Table.SelectRows(PreviousStep, each Date.IsInPreviousNMonths(_[Date], 1))[CA])
    in
        SumPreviousMonth

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

5 Replies

  • Jimmy801's avatar
    Jimmy801
    Icon for Community Champion rankCommunity Champion

    Hello DataVitalizer 

     

    You can filter you table with Table.SelectRows and then sum the CA-column

        SumPreviousMonth = List.Sum(Table.SelectRows(PreviousStep, each Date.IsInPreviousNMonths(_[Date], 1))[CA])

    Here a complete code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TczJDcAwCETRXjhbiCW2SS3I/bdhkILC9Y3+uMOEASJILwrBGQ5PABuSFWjCbiAJqwEnUPuYX7ILVoIiaYEFxPwnTClWci4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CA = _t, Date = _t]),
        PreviousStep = Table.TransformColumnTypes(Source,{{"CA", Int64.Type}, {"Date", type date, "de-DE"}}),
        SumPreviousMonth = List.Sum(Table.SelectRows(PreviousStep, each Date.IsInPreviousNMonths(_[Date], 1))[CA])
    in
        SumPreviousMonth

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

    • DataVitalizer's avatar
      DataVitalizer
      Icon for Super User rankSuper User

      Hi Jimmy801 ,

      I just tried the suggested formula and got 9 as result, although I did not not understand  what the su of 9 represents could that formula be used to return the previous month value for each line instead ?

       

      Thank in you advance.

      • Jimmy801's avatar
        Jimmy801
        Icon for Community Champion rankCommunity Champion

        Hello DataVitalizer 

         

        to show how it works, i just invented a database on my own. So using this table, the result is 9

         

        As you were stating sumx and previous month, i meant, that your result need to be a number (like a measure). But you could also add a column, and replacing the DateTimeFixedLocal with your Date-column.


        If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
        Kudoes are nice too

        Have fun

        Jimmy