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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
larx_mandel
Frequent Visitor

Help build dax using SQL left join

Hi community,

 

I have some tables related in my model, but I cannot find the correct DAX to group them. In SQL I have the following query which gives me the results. My example is a Sales tables, which has the product, each product belongs to a region, each region belong to a state. 

 

I need to obtain the sum of sales by state.

 

SELECT s.Name as State_Name , sum(nr_product_sold) as nr_prod_sold
FROM SALES sales
left join PRODUCT p on sales.id_product = p.id_product
left join REGION r on region.id_region = p.id_region
left join STATES s on s.id_state = r.id_state
group by s.Name

 

Thanks for your help.

2 ACCEPTED SOLUTIONS
ryan_mayu
Super User
Super User

can we use merge queries in PQ, we can do the data transform like left join

https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-shape-and-combine-data?wt.mc_id=DP-M...

 

or you can provide some sample data and the expected output





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

Anonymous
Not applicable

@ryan_mayu  Thanks for your contribution on this thread.

Hi @larx_mandel ,

@ryan_mayu  provided the method in Power Query Editor, here I have another method to help you achieve the requirement. Please follow the steps below to get it:

1. Assume you have the tables SALES, PRODUCT,REGION and STATES in your data model

2. Create a measure as below 

MEASURE =
VAR _statename =
    SELECTEDVALUE ( 'STATES'[Name] )
VAR _stateids =
    CALCULATETABLE (
        VALUES ( 'STATES'[id_state] ),
        FILTER ( 'STATES', 'STATES'[Name] = _statename )
    )
VAR _regionids =
    CALCULATETABLE (
        VALUES ( 'REGION'[id_region] ),
        FILTER ( 'REGION', 'REGION'[id_state] IN _stateids )
    )
VAR _productids =
    CALCULATETABLE (
        VALUES ( 'PRODUCT'[id_product] ),
        FILTER ( 'PRODUCT', 'PRODUCT'[id_region] IN _regionids )
    )
VAR _psold =
    CALCULATE (
        SUM ( 'SALES'[nr_product_sold] ),
        FILTER ( 'SALES', 'SALES'[id_product] IN _productids )
    )
RETURN
    _psold

3. Create table visual and apply the field 'STATES'[Name] and the above measure on it

vyiruanmsft_0-1711705926403.png

Best Regards

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

@ryan_mayu  Thanks for your contribution on this thread.

Hi @larx_mandel ,

@ryan_mayu  provided the method in Power Query Editor, here I have another method to help you achieve the requirement. Please follow the steps below to get it:

1. Assume you have the tables SALES, PRODUCT,REGION and STATES in your data model

2. Create a measure as below 

MEASURE =
VAR _statename =
    SELECTEDVALUE ( 'STATES'[Name] )
VAR _stateids =
    CALCULATETABLE (
        VALUES ( 'STATES'[id_state] ),
        FILTER ( 'STATES', 'STATES'[Name] = _statename )
    )
VAR _regionids =
    CALCULATETABLE (
        VALUES ( 'REGION'[id_region] ),
        FILTER ( 'REGION', 'REGION'[id_state] IN _stateids )
    )
VAR _productids =
    CALCULATETABLE (
        VALUES ( 'PRODUCT'[id_product] ),
        FILTER ( 'PRODUCT', 'PRODUCT'[id_region] IN _regionids )
    )
VAR _psold =
    CALCULATE (
        SUM ( 'SALES'[nr_product_sold] ),
        FILTER ( 'SALES', 'SALES'[id_product] IN _productids )
    )
RETURN
    _psold

3. Create table visual and apply the field 'STATES'[Name] and the above measure on it

vyiruanmsft_0-1711705926403.png

Best Regards

ryan_mayu
Super User
Super User

can we use merge queries in PQ, we can do the data transform like left join

https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-shape-and-combine-data?wt.mc_id=DP-M...

 

or you can provide some sample data and the expected output





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.