Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
DataVitalizer
Super User
Super User

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. 

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community 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

View solution in original post

5 REPLIES 5
Jimmy801
Community Champion
Community 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

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.

Hello @DataVitalizer 

 

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

Jimmy801_0-1600853096761.png

 

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

Hi @Jimmy801 

Below I am sharing a sample, using DAX I was able to create the two caclculated columns but I am trying to reach the same result within Power Query.

So as you can see in each line we are returning the sum either of the prevous month or previous year.

RubriqueIND L1AnneeValuesPrevious MonthPrevious Year
R1IND_11/1/201710000
R1IND_11/2/20171501000
R1IND_11/2/20182000150

 

Thank you in advance.

Hello @DataVitalizer 

 

in DAX you can use relations, what you cannot use in Power query. I've created now 2 tables and in the second table with only  a date-column i added a new column, that sums values of the actual-table, searching for previous-month

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]),
    Actuals = Table.TransformColumnTypes(Source,{{"CA", Int64.Type}, {"Date", type date, "de-DE"}}),
    YourDateTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrTQM7DQMzJQitUBcgz0DCzhHGM9A2MwJxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    ChangedTypeOfDate = Table.TransformColumnTypes(YourDateTable,{{"Date", type date}}),
    AddPreviousMonthToYourTable = Table.AddColumn
    (
        ChangedTypeOfDate,
        "PreviousMonth",
        (row)=>
        List.Sum(Table.SelectRows(Actuals, each _[Date]>= Date.StartOfMonth(Date.AddMonths(row[Date], -1)) and _[Date]<= Date.EndOfMonth( Date.AddMonths(row[Date],-1)))[CA])
    )


in
    AddPreviousMonthToYourTable

 

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors