Min, Max function in power query

thomas819

New Member
Joined
Nov 23, 2020
Messages
36
Office Version
  1. 2019
Platform
  1. Windows
Hello all,
I would like ask you for a help.

This table is very short demonstration of real table which is over 50 000 rows long.
1678636003586.png


Real table is created in power query from multiple txt files and it is connection only.


This is second table which I need to complete. This table is also connection only.
I need to find out minimum and maximum value base on followinf conditions.
1678643933701.png


For Case1 range days 5 -9 including, Case2 range days 8 - 17 and so on.
This is the result which I am loking for:
1678644431805.png


Solution in excel is pretty easy, but I need create a solution in power query.
Excel solution for
Case1 Min
Excel Formula:
=MIN(INDIRECT(ADDRESS(G3;2)):INDIRECT(ADDRESS(H3;2)))
Case1 Max
Excel Formula:
=MAX(INDIRECT(ADDRESS(G3;3)):INDIRECT(ADDRESS(H3;3)))

Unfortunatelly I can't create List.Max function which accept indirect(address():indirect(adrress() as input.
Can you please help me?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This code is pretty fast because it takes advantage of the Day column starting at 1 and incrementing by one each row. If your actual table Day column is the same (i.e., starts at 1 and increments by 1) then this will work. If not, then a modified and slower code would be required.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    lst1 = List.Buffer(List.Zip(List.Skip(Table.ToColumns(Source)))),
    tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    tbl3 = Table.AddColumn(tbl2, "Rcd", each 
            let 
                from = [From],
                to = [To],
                lr = List.Zip(List.Range(lst1, from-1, to - from +1)),
                min = List.Min(lr{0}),
                max = List.Max(lr{1})
            in   
                [Minimum = min, Maximum = max]
    ),
    Result = Table.ExpandRecordColumn(tbl3, "Rcd", {"Minimum", "Maximum"})
in
    Result

Book1
ABCDEFGHIJKL
1Table1Table2Query Output
2
3DayMinimumMaximumFromToFromToMinimumMaximum
412136767917
5247411411117
63101310111011612
74614913913113
856916201620212
96916
1071017
118611
129113
1310712
1411612
1512311
161357
17141020
181545
1916512
2017610
211837
221925
2320211
242117
2522213
26
Sheet1
 
Upvote 0
Solution
This code is pretty fast because it takes advantage of the Day column starting at 1 and incrementing by one each row. If your actual table Day column is the same (i.e., starts at 1 and increments by 1) then this will work. If not, then a modified and slower code would be required.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    lst1 = List.Buffer(List.Zip(List.Skip(Table.ToColumns(Source)))),
    tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    tbl3 = Table.AddColumn(tbl2, "Rcd", each
            let
                from = [From],
                to = [To],
                lr = List.Zip(List.Range(lst1, from-1, to - from +1)),
                min = List.Min(lr{0}),
                max = List.Max(lr{1})
            in  
                [Minimum = min, Maximum = max]
    ),
    Result = Table.ExpandRecordColumn(tbl3, "Rcd", {"Minimum", "Maximum"})
in
    Result

Book1
ABCDEFGHIJKL
1Table1Table2Query Output
2
3DayMinimumMaximumFromToFromToMinimumMaximum
412136767917
5247411411117
63101310111011612
74614913913113
856916201620212
96916
1071017
118611
129113
1310712
1411612
1512311
161357
17141020
181545
1916512
2017610
211837
221925
2320211
242117
2522213
26
Sheet1
Hi JGordon,
you blow my mind away :)

Thank you very much for your answer.

However... Can you please tweak your code a little bit?
Table1
column Day is "M1 Index" in my table (9th column) position can vary
column Minimum is "Low" in my table (6th column) position can vary
column Maximum is "High" in my table (5th column) position can vary

Table2
column From is "M1 Index" in my table (4th column) position can vary
column To is "exit.M1 index" in my table (12th column) position can vary
 
Upvote 0
Hi JGordon,
you blow my mind away :)

Thank you very much for your answer.

However... Can you please tweak your code a little bit?
Table1
column Day is "M1 Index" in my table (9th column) position can vary
column Minimum is "Low" in my table (6th column) position can vary
column Maximum is "High" in my table (5th column) position can vary

Table2
column From is "M1 Index" in my table (4th column) position can vary
column To is "exit.M1 index" in my table (12th column) position can vary
Hi, I understand your code and I changed it accordingly.

Thank you once more.
 
Upvote 0

Forum statistics

Threads
1,215,131
Messages
6,123,223
Members
449,091
Latest member
jeremy_bp001

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top