Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

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},

 

 

1 ACCEPTED SOLUTION
123abc
Community Champion
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.

 

View solution in original post

4 REPLIES 4
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!

123abc
Community Champion
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
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.

123abc
Community Champion
Community Champion

You are always wellcome.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors