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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Creating a new Calculated Table using while loops

Hello, I am currently trying to make a new table from a data set I have. The data I have is basically this, a length measurement associated with a classification (1 or 2). The data is orginized in a way that the length increments are about 500 ft. and there are about a dozen or so in a row of the same classification before it changes to the other class. What I want to do is create a new table with "segment numbers" for each time the classification changes and get a sum of all those length measurements for that segment.

 

The left table is what I have, and the right table is what I want. If this is possible, and anyone can help me that would be great

Capture.PNG

1 ACCEPTED SOLUTION
PaulDBrown
Community Champion
Community Champion

Here is one way.

In Power Query, create the segment table by using the equivalent to the following in a blank query:

let
    Source = FTable, // Change "FTable" to point at your fact table
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Class"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
    #"Inserted Merged Column" = Table.AddColumn(#"Removed Duplicates", "Segment", each Text.Combine({"Segment ", Text.From([Class], "es-ES")}), type text)
in
    #"Inserted Merged Column"

Once loaded into the model, create a one-to-many relationship between the new Class table and the fact table.

Model.png

 

Now you can create a SUm measure for length, and create a table visual using the Segment field and the [Sum length] measure

result.png

 

I've attached the cample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

3 REPLIES 3
PaulDBrown
Community Champion
Community Champion

Here is one way.

In Power Query, create the segment table by using the equivalent to the following in a blank query:

let
    Source = FTable, // Change "FTable" to point at your fact table
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Class"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
    #"Inserted Merged Column" = Table.AddColumn(#"Removed Duplicates", "Segment", each Text.Combine({"Segment ", Text.From([Class], "es-ES")}), type text)
in
    #"Inserted Merged Column"

Once loaded into the model, create a one-to-many relationship between the new Class table and the fact table.

Model.png

 

Now you can create a SUm measure for length, and create a table visual using the Segment field and the [Sum length] measure

result.png

 

I've attached the cample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

This is almost what I was hoping for, except the "class" in the original dataset is only 1 or 2. I'm hoping to name segments based on the chronological order the classes come in, ie. the first 6 instances of class 1 is segment 1, then the next 4 rows of class 2 is segment 2, the next rows of class 1 is segment 3 etc.

Big thumbs up to @Vijay_A_Verma for helping with the code to establish the consecutive segments.

result.png

 

File attached





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.