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

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.

Reply
Anonymous
Not applicable

Compare Any 2 non-consecutive Rows (PowerQuery)

Hi Guys,

 

I need to compare the Count values by Sector at every Time interval (e.g. row at Index 2 with row at Index 7) and output the result in Count.1 column.

- These Rows may be at different Index distances at every run (depending on sector's data received).

- The Sector values itself may vary.

- After the first 1-2 rows (always 0 Count value) the Time interval is constant every 10s.

I cannot play with custom Index columns given the requirements mentioned above, so I came up with the below solution (Count.1 values in the Table below are the correct output):

 

#"Added Count.1" = Table.AddColumn(#"Previous step", "Count.1", (a) =>

  let   

    b = Table.SelectRows(#"Previous step", (x) => x[Sector] = a[Sector] and x[Time] = a[Time]+10)  

  in

    b[#"Count"]{0} 

  )

 

But this is veeery slow, and I'm using test data (300 rows), when the report will be live the number of rows will grow exponentially. Any idea how to improve my code to a more efficient/fast way?

 

Sample data:

 

IndexTimeSectorCountCount.1
01593018439   20000
1159301844020000
215930184504198208
315930184505176214
415930184506176180
515930184503176180
615930184501178216
715930184604208208
815930184605214220
915930184606180180
1015930184603180180
1115930184601216220
1215930184701220220
1315930184706180180
1415930184703180180
1515930184704208208
1615930184705220220
1715930184804208208
1815930184805220220
1915930184806180180
2015930184803180180
2115930184801220220

 

Thanks:)

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

IMHO, it's Table.SelectRows that slows things down. I rewrite the logic with Table.Group; it turns out to be a bit faster now. (I tested a mockup of 1,500 rows; loading time is around 7~8s)

I'm also looking forward to gurus' better solutions.

Mine is for your reference,

 

let
Source = Excel.CurrentWorkbook(){[Name="Log"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Time", Int64.Type}, {"Sector", Int64.Type}, {"Count", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Count.1", each
[sub1 = Table.Group(#"Changed Type",{"Sector"},{{"Count", each _}}),
sub2 = sub1{[Sector=[Sector]]}[Count],
sub3 = sub2{[Time=[Time]+10]}[Count]][sub3])
in
#"Added Custom"

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

6 REPLIES 6
ziying35
Impactful Individual
Impactful Individual

@Anonymous 

Right, don't make duplicate posts, just one post for each question. I didn't notice you made a duplicate post, mark the post as solved or delete it.

edhans
Super User
Super User

@Anonymous  this is a duplicate post. Can you either mark this thread as solved, or delete your original post? Thanks. Not sure if you do that if @ziying35 @CNENFRNL  and @lbendlin  have to delete theirs too or as the OP you delete the whole thread....

 

Original and solved post here.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

@edhans  @ziying35 

 

Sorry guys, I didn't realize it had been published twice, the first time didn't go through (it didn't appear anywhere) so I re-wrote it.

I'm not able to delete this post now so I marked it as solved.

CNENFRNL
Community Champion
Community Champion

IMHO, it's Table.SelectRows that slows things down. I rewrite the logic with Table.Group; it turns out to be a bit faster now. (I tested a mockup of 1,500 rows; loading time is around 7~8s)

I'm also looking forward to gurus' better solutions.

Mine is for your reference,

 

let
Source = Excel.CurrentWorkbook(){[Name="Log"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Time", Int64.Type}, {"Sector", Int64.Type}, {"Count", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Count.1", each
[sub1 = Table.Group(#"Changed Type",{"Sector"},{{"Count", each _}}),
sub2 = sub1{[Sector=[Sector]]}[Count],
sub3 = sub2{[Time=[Time]+10]}[Count]][sub3])
in
#"Added Custom"

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

ziying35
Impactful Individual
Impactful Individual

Hi, @CNENFRNL 

Yes, the Table.SelectRows function is one of the less efficient functions ir Power Query, but sometimes you have to use it to process data.

 

Can you clear out the sensitive information inside the form, upload the file to the cloud drive, and share the link here?l'd like to see if there's a way to optimize the code based on the actual amount of data.

lbendlin
Super User
Super User

Ask yourself if you really have to do this in Power Query or if this can be done at the data source?

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Kudoed Authors