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.
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

peter789

Board Regular
Joined
Nov 20, 2016
Messages
114
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
 

Watch MrExcel Video

Forum statistics

Threads
1,099,249
Messages
5,467,521
Members
406,544
Latest member
semoredhawk

This Week's Hot Topics

Top