Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Multiple conditions in aggregations

Hi

the code below written by ronrsnfld 

See original post here:

Solved: Re: Average records before and after given date - Microsoft Fabric Community

 

 

        {"Repair Date", each [Repair Date]{0}, type date},

        {"avg 3 repairs cost before",  (t)=> 
            List.Average(
                List.LastN(
                    Table.SelectRows(t, each [Failure Date] < [Repair Date])[Repair Cost],
                3)
            ), type number},

 

 

The code accommodates for one condition:

 

each [Failure Date] < [Repair Date])[Repair Cost]

 

 

what if AND / OR conditions are required? what would be the revised code?

I've tried the following but it didn't works!

 

        {"Repair Date", each [Repair Date]{0}, type date}, {"Insured Date", each [Insured Date]{0}, type date}

        {"avg 3 repairs cost before",  (t)=> 
            List.Average(
                List.LastN(
                    Table.SelectRows(t, each [Failure Date] < [Repair Date])[Repair Cost] and [Failure Date] < [Insured Date])[Repair Cost],
                3)
            ), type number},

 

 

  • In Power Query M language, you can use the "and" or "or" conditions to combine multiple conditions. However, it looks like you have a small syntax error in your code. You should use the "and" operator within the Table.SelectRows function. Here's the revised code with both conditions:

     

    {"Repair Date", each [Repair Date]{0}, type date},
    {"Insured Date", each [Insured Date]{0}, type date},
    {"avg 3 repairs cost before", (t)=>
    List.Average(
    List.LastN(
    Table.SelectRows(t, each [Failure Date] < [Repair Date] and [Failure Date] < [Insured Date])[Repair Cost],
    3
    )
    ), type number
    }

     

    In the revised code, I've used the "and" operator to ensure that both conditions [Failure Date] < [Repair Date] and [Failure Date] < [Insured Date] are met when filtering the rows using Table.SelectRows. This should calculate the average of "Repair Cost" for rows that satisfy both conditions.

     

     

    If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

     

    In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

     

4 Replies

  • 123abc's avatar
    123abc
    Community Champion

    In Power Query M language, you can use the "and" or "or" conditions to combine multiple conditions. However, it looks like you have a small syntax error in your code. You should use the "and" operator within the Table.SelectRows function. Here's the revised code with both conditions:

     

    {"Repair Date", each [Repair Date]{0}, type date},
    {"Insured Date", each [Insured Date]{0}, type date},
    {"avg 3 repairs cost before", (t)=>
    List.Average(
    List.LastN(
    Table.SelectRows(t, each [Failure Date] < [Repair Date] and [Failure Date] < [Insured Date])[Repair Cost],
    3
    )
    ), type number
    }

     

    In the revised code, I've used the "and" operator to ensure that both conditions [Failure Date] < [Repair Date] and [Failure Date] < [Insured Date] are met when filtering the rows using Table.SelectRows. This should calculate the average of "Repair Cost" for rows that satisfy both conditions.

     

     

    If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

     

    In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      123abc

      Thanks a lot.

      I had done multiple code corrections but seems I missed parenthesizes 🙂

      It is working now. thanks for your prompt support.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Oh by the way y'all, I've been getting the first column values by using each Table.FirstN(Table, 1)[Insured Date]{0}. Also Table.FirstValue([[Column]]) is a good choice as well.

     

    --Nate 

     

    It's optimized somehow--test it out!