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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
ruut
Frequent Visitor

SQL->DAX: How to create a self-joined table with greater than sign in join

I have written the following Access SQL query:

SELECT 
  t1.Field2, 
  t1.Field1 AS Field1A, 
  t2.Field1 AS Field1B, 
  sum(t2.Field4) AS Summed
FROM
  Table1 AS t1 
INNER JOIN 
  Table1 AS t2 
      ON 
         t1.Field1 >= t2.Field1 AND 
         t1.Field2 = t2.Field2 AND 
         t1.Field3 = t2.Field3
WHERE 
  t1.Field3 = 1
GROUP BY 
  t1.Field2, 
  t1.Field1, 
  t2.Field1

which I would like to rewrite in Power BI DAX language. How to get started?

 

Note that Table1 is joined with itself and note the '>=' sign in the INNER JOIN condition.

 

Your help would be appreciated a lot. 

 

1 ACCEPTED SOLUTION
v-ljerr-msft
Microsoft Employee
Microsoft Employee

Hi @ruut,

 

After a few research and test, I found a solution using M query instead DAX in your scenario. 

 

1. Duplicate Table1 in Query Editor.

d1.PNG

 

2. Create a Blank Query, and enter the following M query in Advanced Editor.

let
    RelativeMerge = Table.AddColumn(Table1, "RelativeJoin", 
            (Earlier) => Table.SelectRows(Table2, 
                         each [Field1]<=Earlier[Field1] and [Field2]=Earlier[Field2] and [Field3]=Earlier[Field3])),
    #"Expanded RelativeJoin" = Table.ExpandTableColumn(RelativeMerge, "RelativeJoin", {"Field1", "Field4"}, {"RelativeJoin.Field1", "RelativeJoin.Field4"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded RelativeJoin",{"Field2", "Field1", "Field3", "Field4", "RelativeJoin.Field1", "RelativeJoin.Field4"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Field3", "Field4"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Field1", "Field1A"}, {"RelativeJoin.Field1", "Field1B"}, {"RelativeJoin.Field4", "Summed"}})
in
   #"Renamed Columns"

blankquery.PNG

 

Here is the sample pbix file for your reference. Smiley Happy

 

Regards

View solution in original post

5 REPLIES 5
v-ljerr-msft
Microsoft Employee
Microsoft Employee

Hi @ruut,

 

After a few research and test, I found a solution using M query instead DAX in your scenario. 

 

1. Duplicate Table1 in Query Editor.

d1.PNG

 

2. Create a Blank Query, and enter the following M query in Advanced Editor.

let
    RelativeMerge = Table.AddColumn(Table1, "RelativeJoin", 
            (Earlier) => Table.SelectRows(Table2, 
                         each [Field1]<=Earlier[Field1] and [Field2]=Earlier[Field2] and [Field3]=Earlier[Field3])),
    #"Expanded RelativeJoin" = Table.ExpandTableColumn(RelativeMerge, "RelativeJoin", {"Field1", "Field4"}, {"RelativeJoin.Field1", "RelativeJoin.Field4"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded RelativeJoin",{"Field2", "Field1", "Field3", "Field4", "RelativeJoin.Field1", "RelativeJoin.Field4"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Field3", "Field4"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Field1", "Field1A"}, {"RelativeJoin.Field1", "Field1B"}, {"RelativeJoin.Field4", "Summed"}})
in
   #"Renamed Columns"

blankquery.PNG

 

Here is the sample pbix file for your reference. Smiley Happy

 

Regards

Hi @v-ljerr-msft ,

The approach which you have suggested is greate like taek duplicate data set then join then select what ever the colulmns we requeid.

 

But the same thing we can achieve through DAX also . But what is the difference and which one is fater and best practice .

 

Please suggest.

Hi @v-ljerr-msft

 

Thanks for the solution. This is exactly what I needed. For lager input tables, the query is quite slow. Do you have any suggestions to increase the performance? 

 

Best, 

 

Ruut

fhill
Resident Rockstar
Resident Rockstar

Can you post sample table data and your desired output?

 

FOrrest




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




ruut
Frequent Visitor

Input:

2017-10-06 17_15_34-Access - PECO_PhaseIII_BudgetTracker - many hidden objects _ Database- C__key ar.png

Output:

2017-10-09 10_39_27-Access - PECO_PhaseIII_BudgetTracker - many hidden objects _ Database- C__key ar.png

 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.