Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.
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!
Solved! Go to Solution.
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.
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-...
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/1447...
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 |
Hi,
Any specific reason for doing this in the Query Editor? Why not do this in DAX with a calculated column formula?
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"
Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!
Check out the September 2025 Power BI update to learn about new features.