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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Raitup00
Regular Visitor

Subqueries in Power BI

Hi everyone

 

I have the following query in SQL:

SELECT * FROM population
WHERE life_expectancy > 1.15 *

(SELECT AVG(life_expectancy)
FROM population
WHERE year = 2015)
AND year = 2015;

 

It is a subquery and I'm trying to get the same result but in Power query.

 

This is an extract of the table

Raitup00_0-1644203256151.png

 

How can do that in Power query?

 

Thanks in advance

 

Ray

 

 

 

 

 

2 ACCEPTED SOLUTIONS
AlexisOlson
Super User
Super User

In M, the queries are typically written so that each line is basically a subquery that usually references the previous line but you can reference other lines too.

 

Your query might look something like this:

let
    Source = ( Your Data Source ),
    #"Filtered Year" = Table.SelectRows(Source, each ([Year] = 2015)),
    #"Calculated Average" = List.Average(#"Filtered Year"[life_expectancy]),
    #"Filtered Table" = Table.SelectRows(Source, each [life_expectancy] > 1.15 * #"Calculated Average")
in
    #"Filtered Table"

Each line references the previous one except for #"Filtered Table", which references two prior lines (Source, and #"Calculated Average").

View solution in original post

v-eqin-msft
Community Support
Community Support

Hi @Raitup00 ,

 

 Based on @AlexisOlson 's suggestion ,please paste the whole M to Advanced Editor dialog:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TdBLasQwDAbgu3g9CEnWw1rO0HY2pYVuugi5/zVqySljCBGJv/yRdByNsd3a/fE774yUDwSOMqsLhOYzkoa383Y0ih3XIZh4YgX1wjxGFOa0H89XsIKLZR0QadnDmPGK3nUmC5j2WQ3B87Czsqnx0hX+/H6FGwhVOM7v8jUxRagsTjvX6iWiOINlZcXpffGc/v752Fdilr24A2bloD594b7jtRKPwgMw/5sLIbumrG1/ve3RnLuY1xChtbnYka7TDETxvhrEIj/ve86wkcjAMnB0DtF/PnZ+NVlsTqQ5bpBazBnP8w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [pop_id = _t, country_code = _t, year = _t, fertility_rate = _t, life_expectancy = _t, size = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"pop_id", Int64.Type}, {"country_code", type text}, {"year", Int64.Type}, {"fertility_rate", type number}, {"life_expectancy", type number}, {"size", Int64.Type}}),
    #"Added Custom" =Table.AddColumn(#"Changed Type","Avg *1.15",each List.Average(Table.SelectRows(#"Changed Type" ,each [year]=2015)[life_expectancy]) *1.15   ) ,
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Avg *1.15", type number}}),
    #"Filtered Rows"=Table.SelectRows(#"Changed Type1", each [year]=2015 and [life_expectancy]> [#"Avg *1.15"])
in
    #"Filtered Rows"

Output:

Eyelyn9_0-1644479959775.png

 

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-eqin-msft
Community Support
Community Support

Hi @Raitup00 ,

 

 Based on @AlexisOlson 's suggestion ,please paste the whole M to Advanced Editor dialog:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TdBLasQwDAbgu3g9CEnWw1rO0HY2pYVuugi5/zVqySljCBGJv/yRdByNsd3a/fE774yUDwSOMqsLhOYzkoa383Y0ih3XIZh4YgX1wjxGFOa0H89XsIKLZR0QadnDmPGK3nUmC5j2WQ3B87Czsqnx0hX+/H6FGwhVOM7v8jUxRagsTjvX6iWiOINlZcXpffGc/v752Fdilr24A2bloD594b7jtRKPwgMw/5sLIbumrG1/ve3RnLuY1xChtbnYka7TDETxvhrEIj/ve86wkcjAMnB0DtF/PnZ+NVlsTqQ5bpBazBnP8w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [pop_id = _t, country_code = _t, year = _t, fertility_rate = _t, life_expectancy = _t, size = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"pop_id", Int64.Type}, {"country_code", type text}, {"year", Int64.Type}, {"fertility_rate", type number}, {"life_expectancy", type number}, {"size", Int64.Type}}),
    #"Added Custom" =Table.AddColumn(#"Changed Type","Avg *1.15",each List.Average(Table.SelectRows(#"Changed Type" ,each [year]=2015)[life_expectancy]) *1.15   ) ,
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Avg *1.15", type number}}),
    #"Filtered Rows"=Table.SelectRows(#"Changed Type1", each [year]=2015 and [life_expectancy]> [#"Avg *1.15"])
in
    #"Filtered Rows"

Output:

Eyelyn9_0-1644479959775.png

 

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

AlexisOlson
Super User
Super User

In M, the queries are typically written so that each line is basically a subquery that usually references the previous line but you can reference other lines too.

 

Your query might look something like this:

let
    Source = ( Your Data Source ),
    #"Filtered Year" = Table.SelectRows(Source, each ([Year] = 2015)),
    #"Calculated Average" = List.Average(#"Filtered Year"[life_expectancy]),
    #"Filtered Table" = Table.SelectRows(Source, each [life_expectancy] > 1.15 * #"Calculated Average")
in
    #"Filtered Table"

Each line references the previous one except for #"Filtered Table", which references two prior lines (Source, and #"Calculated Average").

Greg_Deckler
Super User
Super User

@Raitup00 Just create a step in Advanced Editor where you calculate the average of life expectancy in 2015 and multiply by 1.15. Then you can create a column that figures out if the life expectancy is greater than that and filter to those rows.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

 

Thank for your prompt reply. 

 

I'm not an expert in the Advanced editor. I added a calculated column using List.Average but the first argument is indeed a list, the values in the column "life_expectancy" is not a list. 

 

Could you give a hint or idea how to insert a step using Advanced editor?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors