Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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:)

  • 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"

6 Replies

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

  • CNENFRNL's avatar
    CNENFRNL
    Icon for Community Champion rankCommunity 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"
    • ziying35's avatar
      ziying35
      Icon for Impactful Individual rankImpactful 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.

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

  • ziying35's avatar
    ziying35
    Icon for Impactful Individual rankImpactful 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.