Writing Power Query (M language) Table.X functions that will dynamically tailor the table scope to each row of data

Alex MEDC

New Member
Joined
May 16, 2019
Messages
2
I have loaded a simple flat file I have pulled into a query. The data looks like this:


A
B
C
D
E
F
1
Broad Identifier
Specific Identifier
Candidate
Candidate Score
Qualifier
Candidate Score Evaluation, Desired Outcome
2
9
9.333
9.111
0.222
3
9
9.333
9.111
0.222
4
9
9.333
9.777
1.444
Ignore
5
9
9.333
9.333
Lowest
6
9
9.333
9.555
1.222
7
14
14.111
14.222
1.111
Lowest
8
14
14.111
14.111
Ignore
9
14
14.111
14.333
1.222
10
14
14.111
14.444
1.333
11
9
9.444
9.111
0.333
12
9
9.444
9.399
0.045
Ignore
13
9
9.444
9.111
0.333
14
9
9.444
9.333
0.111
Lowest
15
9
9.444
9.555
1.111
16
9
9.777
9.111
0.666
17
9
9.777
9.111
0.666
18
9
9.777
9.333
0.444
19
9
9.777
9.555
0.222
Lowest
20
9
9.777
9.599
0.178
Ignore
21
9
9.777
9.888
1.111
Ignore

<tbody>
</tbody>
Data2



Note that the real input data is in columns A:E; column F is just a hand-typed set of values that I am HOPING I can achieve with M code, with your help.

Here is the M code for the full query taken from the Power Query editor:

Code:
let
    Source = Csv.Document(File.Contents("H:\Misc\Power Query experiment\Data2.csv"),[Delimiter=",", Columns=6, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Broad Identifier", Int64.Type}, {"Specific Identifier", type number}, {"Candidate", type number}, {"Candidate Score", type number}, {"Qualifier", type text}, {"Candidate Score Evaluation, Desired Outcome", type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Candidate Score Evaluation", each if [Candidate Score] = List.Min(Table.SelectRows(#"Changed Type",
                                                                                                                                                       each ([Specific Identifier] = [Specific Identifier] and [Qualifier] <> "Ignore"))[Candidate Score])
                                                                                                    then "Lowest"
                                                                                                    else null)
in
    #"Added Conditional Column"

You will notice from the code that the column added in the final step is intended to, for each row, (a) focus only on rows in the table that share the same value in the "Specific Identifier" column as the current row and (b) ignore rows that have Ignore in the "Qualifier" column. If you try this code out on the data, you'll see that it does not achieve the desired results.

Please help.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
It's not pretty but this is one way of doing it.
First filter out ignore. Then replace null with zero if that is what you mean to do, otherwise filter out null.
Then reference this query.
Then group this query by Specific Identifier and return the minimum Candidate score and give the query a meaningful name.
Merge the two queries linking on the Specific identifier and Candidate score (use Ctrl to select second link).
Expand minimum.
Filter by not null.
Add custom column with formula ="Lowest".
Go back to original query, merge as new with the the query which has the column with Lowest in it.
Link again on Specific Identifier and Candidate Score.
Expand table to only return Custom column which will add Lowest.
Then tidy up by removing unwanted columns.
Peter
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,163
Members
448,554
Latest member
Gleisner2

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