Forum Discussion
M Query Formula to bring Previous Month Segment
- Anonymous2 years ago
Hi Anonymous ,
Open "Advanced Editor" and copy and paste the following code.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJW0lFyBOLgxJzUYqVYHRQJJxQJI1QdjsnJ+aV5JRhyqJqMERLOQOybWJSdWpKZl44u6YIhaYKqE8U6E5waYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Parse = _t, #"Emp code" = _t, Segment = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Parse", type date}, {"Emp code", type text}, {"Segment", type text}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Emp code"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1),type table}}), #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Parse", "Segment", "Index"}, {"Parse", "Segment", "Index"}), #"Added Custom" = Table.AddColumn(#"Expanded Count", "Custom", each if [Index]<>1 then List.Min(Table.SelectRows(#"Expanded Count",(x)=>x[Emp code]=[Emp code] and x[Index]=[Index]-1)[Segment]) else null), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each let _maxindex=List.Max(Table.SelectRows(#"Added Custom",(x)=>x[Emp code]=[Emp code])[Index]) in if [Custom]=null then 0 else if [Index]=_maxindex and [Custom]=[Segment] then 0 else if [Index]=_maxindex and [Custom]<>[Segment] then 1 else null), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.2", each let _maxindex=List.Max(Table.SelectRows(#"Added Custom",(x)=>x[Emp code]=[Emp code])[Index]), _count=Table.RowCount(Table.SelectRows(#"Added Custom1",(x)=>x[Emp code]=[Emp code] and x[Custom.1]=1)) in if [Index]<>_maxindex and _count>0 then -1 else [Custom.1]), #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Index", "Custom", "Custom.1"}) in #"Removed Columns"Then the result is as follows.
Best Regards,
Neeko Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information or anything not related to the issue or question.
If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
Please show the expected outcome based on the sample data you provided.
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523
- Anonymous2 years agoNot applicable
Hi,
Thanks for replying here's the sample data:
Note: i have a monthly data upto 12 months of rows average 300 for each month.Parse Emp code Segment 1/1/2023 A Sales 1/1/2023 B Sales 1/2/2023 A Accounts 1/2/2023 B Sales 1/3/2023 C Marketing 1/3/2023 D Marketing 1/4/2023 C Accounts 1/4/2023 D Marketing
Here's the Expected output first if there's a segment change for a employee then his previous month segment should be in the newly changed segment month and another column i need to create is the segment count if there's no change 0, if change then new segment month = 1 and previous month = -1,
Please look at the tables and columns (parse, emp code ) more clearly for better understanding.Parse Emp code Segment Segment Name Tranfer Count 1/1/2023 A Sales -1 1/1/2023 B Sales 0 1/2/2023 A Accounts Sales 1 1/2/2023 B Sales 0 1/3/2023 C Marketing -1 1/3/2023 D Marketing 0 1/4/2023 C Sales Marketing 1 1/4/2023 D Marketing 0 - lbendlin2 years ago
Super User
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJW0lFyBOLgxJzUYqVYHRQJJzQJI2QdjsnJ+aV5JRhy6JqMYRLOQOybWJSdWpKZl44u6YJF0gRZJ5p1Jjg0xgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Parse = _t, #"Emp code" = _t, Segment = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Parse", type date}, {"Emp code", type text}, {"Segment", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Prior Segment", (k)=> try Table.LastN(Table.SelectRows(#"Changed Type", each [Emp code]=k[Emp code] and [Parse]<k[Parse]),1)[Segment]{0} otherwise null) in #"Added Custom" - Ashish_Mathur2 years ago
Super User
Hi,
Any specific reason for doing this in the Query Editor? Why not do this in DAX with a calculated column formula?