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.
Hi Community,
Is there any alterantive way to rewrite the below formula in Language M:
CALCULATE(SUM[CA],PREVIOUSMONTH([Date]))
Thank you in advance.
Solved! Go to Solution.
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
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
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.
Rubrique | IND L1 | Annee | Values | Previous Month | Previous Year |
R1 | IND_1 | 1/1/2017 | 100 | 0 | 0 |
R1 | IND_1 | 1/2/2017 | 150 | 100 | 0 |
R1 | IND_1 | 1/2/2018 | 200 | 0 | 150 |
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
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
100 | |
51 | |
19 | |
12 | |
11 |