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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Babycakes_00
Helper I
Helper I

Deciphering SQL to Power Query Steps

Hi all

 

I am unfamiliar with SQL script and have a set of data where the following has been used "

@row_number := CASE

            WHEN @prev_id = t1.id THEN @row_number + 1

 

  1. Add an Index Column:

    • Go to the “Add Column” tab.
    • Select “Index Column” and choose “From 1”.
  2. Add a Custom Column for Row Number:

    • Add a custom column to calculate the row number for each ID:
      = Table.AddColumn(PreviousStep, "RowNumber", each if [ID] = try PreviousStep{[Index]-2}[ID] otherwise null then [Index] - try PreviousStep{[Index]-2}[Index] otherwise 1)

 

  1. Group by ID and Calculate Max Count:
    • Group the table by ID and calculate the maximum row number for each ID:
      = Table.Group(PreviousStep, {"ID"}, {{"MaxCount", each List.Max([RowNumber]), type number}})

 

  1. Join the Aggregated Data Back to the Main Query:
    • Join the main query with the aggregated data on ID:
      = Table.Join(MainQuery, "ID", AggregatedData, "ID")

 

  1. Calculate Final Minutes:
    • Add a custom column to calculate the final Minutes:
      = Table.AddColumn(PreviousStep, "FinalMinutes", each [Minutes] / [MaxCount])
3 REPLIES 3
lbendlin
Super User
Super User

@row_number := CASE

            WHEN @prev_id = t1.id THEN @row_number + 1

Please provide the entire SQL - missing the ELSE and END statements.

AS max_counts ON J1.ID = max_counts.ID;

That is some rather advanced SQL.  Can you confirm that this runs in SSMS?

 

I would not be surprised if things like 

 

...

@row_number := CASE

                WHEN @prev_id = t1.id THEN @row_number + 1

                ELSE 1

            END AS `COUNT`,

            @prev_id := t1.id

...

 

would not be supported in Power Query's SQL connector (unless you declare it as Value.NativeQuery maybe).

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.