Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Maxifs - Power Query

Hello Guys  πŸ™‚   Please, I need your help regarding the use maxifs in the power query    I need your help to translate this function into power query language:   =IF(MAXIFS($C$2:$C$7,$A$2:$A$7,...
  • edhans's avatar
    edhans
    5 years ago

    Ok Anonymous - it isn't a function per se, but a series of functions using Table.Group and Table.Max. The code is below. It turns this:

    into this:

     

    It does it by grouping by the contract and determing the max Evaluation Date, then comparing it to each date for a given contract, returning true or false. These are real true/false values (like Excel's TRUE() and FALSE()). 

     

    I used my own data because you posted data in an image, which is great for expected results, but I cannot copy and paste it in. The links I provided earlier show you how to supply data to the forum in a usable format.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WqlDSUTLUN9Q3MjAyQGbG6mCRM0bIVQIFjBByRjjkLIFME7iZsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Contract = _t, #"Visit Date" = _t, #"Evaluation Date" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Visit Date", type date}, {"Evaluation Date", type date}}),
        #"Grouped Rows" = 
            Table.Group(
                #"Changed Type", 
                {"Contract"}, 
                {
                    {
                        "AllRows", 
                        each 
                            let
                                varTable = _
                            in
                        Table.AddColumn(
                            _, 
                            "Max Date", 
                            each 
                                let
                                    varDate = [Evaluation Date]
                                in
                                Table.Max(varTable, "Evaluation Date")[Evaluation Date] = varDate
                            ), type table [Contract=nullable text, Visit Date=nullable date, Evaluation Date=nullable date, Max Date = nullable date]}
                }
            ),
        #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Visit Date", "Evaluation Date", "Max Date"}, {"Visit Date", "Evaluation Date", "Max Date"})
    in
        #"Expanded AllRows"

     

    How to use M code provided in a blank query:
    1) In Power Query, select New Source, then Blank Query
    2) On the Home ribbon, select "Advanced Editor" button
    3) Remove everything you see, then paste the M code I've given you in that box.
    4) Press Done
    5) See this article if you need help using this M code in your model.