Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

M Query Formula to bring Previous Month Segment

I have used this formula using ChatGPT:
let
CustomSegment = Table.AddColumn(#"Table Name", "Segment name T", each
if [Custom] = 1 then
let
EmpCode = [Employee Code], // Store the current Employee Code
PrevMonth = Date.AddMonths([Parse], -1), // Get the previous month based on the 'Parse' column
PrevMonthData = Table.SelectRows(#"Table Name", each [Employee Code] = EmpCode and Date.Year([Parse]) = Date.Year(PrevMonth) and Date.Month([Parse]) = Date.Month(PrevMonth)), // Filter for rows with the same Emp Code in the previous month
SegmentName = if Table.RowCount(PrevMonthData) > 0 then PrevMonthData{0}[Segment] else null // Get the Segment value if there's a matching row; otherwise, return null
in
SegmentName
else
null // If Custom is not equal to 1, return null
)
in
CustomSegment

I have monthly data consolidated where i have created a column named "Custom" so if it is "1" then look for the previous month of the same emp code and bring its Segment name from the Segment column else null.

This formula throws an Cyclic reference error! Please help me to fix this!

  • Anonymous's avatar
    Anonymous
    2 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. 

5 Replies

  • 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

    • Anonymous's avatar
      Anonymous
      Not 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.

      ParseEmp codeSegment
      1/1/2023ASales
      1/1/2023BSales
      1/2/2023AAccounts
      1/2/2023BSales
      1/3/2023CMarketing
      1/3/2023DMarketing
      1/4/2023CAccounts
      1/4/2023DMarketing


      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.



      ParseEmp codeSegmentSegment NameTranfer Count
      1/1/2023ASales -1
      1/1/2023BSales 0
      1/2/2023AAccountsSales1
      1/2/2023BSales 0
      1/3/2023CMarketing -1
      1/3/2023DMarketing 0
      1/4/2023CSalesMarketing1
      1/4/2023DMarketing 0
      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper 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"
  • Anonymous's avatar
    Anonymous
    Not applicable

    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.