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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
baribir
Helper I
Helper I

Create a new table from two other tables

Hi All,

 

I need to make a new table from the contents of two other tables with DAX. And add a column with the time difference(Date1-Date2).

 

Table 1:

ID1Date1
105-12-18
204-12-18
304-12-18
404-12-18

 

Table2:

 

ID2Date2
120-11-18
227-11-18
304-12-18
411-12-18

 

A new table:

 

 

ID1Date1ID2Date2Diff
105-12-18120-11-1815
105-12-18227-11-188
105-12-18304-12-181
105-12-18411-12-18-6
204-12-18120-11-1814
204-12-18227-11-187
204-12-18304-12-180
204-12-18411-12-18-7
304-12-18120-11-1814
304-12-18227-11-187
304-12-18304-12-180
304-12-18411-12-18-7
404-12-18120-11-1814
404-12-18227-11-187
404-12-18304-12-180
405-12-18411-12-18-6

  

 

3 ACCEPTED SOLUTIONS
GilbertQ
Super User
Super User

Hi there

 

 

Here is the DAX code

 

Table = CROSSJOIN('Table1','Table2')

This will give you the result you want

 

 





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

Proud to be a Super User!







Power BI Blog

View solution in original post

Here we go

 

Table =
CALCULATETABLE (
    ADDCOLUMNS (
        CROSSJOIN ( 'Table1', 'Table2' ),
        "DateDiff", DATEDIFF ( 'Table1'[Date1], 'Table2'[Date2], DAY )
    )
)

image.png





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

Proud to be a Super User!







Power BI Blog

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

 

Just in case you want to do this with M code, try this

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1TU00jUyMLRQitWJVjICCZmgCBljCpmgCcUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID1 = _t, Date1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID1", Int64.Type}, {"Date1", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table2),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"ID2", "Date2"}, {"ID2", "Date2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"ID2", Int64.Type}}),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type1", {{"Date2", type date}}, "en-IN"),
    #"Added Custom1" = Table.AddColumn(#"Changed Type with Locale", "Custom", each [Date1]-[Date2]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom1",{{"Custom", Int64.Type}})
in
    #"Changed Type2"

You may download my PBI file from here.

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

10 REPLIES 10
Ashish_Mathur
Super User
Super User

Hi,

 

Just in case you want to do this with M code, try this

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1TU00jUyMLRQitWJVjICCZmgCBljCpmgCcUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID1 = _t, Date1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID1", Int64.Type}, {"Date1", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table2),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"ID2", "Date2"}, {"ID2", "Date2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"ID2", Int64.Type}}),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type1", {{"Date2", type date}}, "en-IN"),
    #"Added Custom1" = Table.AddColumn(#"Changed Type with Locale", "Custom", each [Date1]-[Date2]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom1",{{"Custom", Int64.Type}})
in
    #"Changed Type2"

You may download my PBI file from here.

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur,

 

Thank you!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
GilbertQ
Super User
Super User

Hi there

 

 

Here is the DAX code

 

Table = CROSSJOIN('Table1','Table2')

This will give you the result you want

 

 





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

Proud to be a Super User!







Power BI Blog

Thanks @GilbertQ,

 

Could you advise how to find the also with Dax add a new column with datediff.  

Hi there

 

You should be able to do it with a DATEDIFF(StartDate,EndDate,INTERVAL)





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

Proud to be a Super User!







Power BI Blog

@GilbertQ,

 

At a single DAX expression. I want to use this as a var in other expression to filter by date and datediff.

Here we go

 

Table =
CALCULATETABLE (
    ADDCOLUMNS (
        CROSSJOIN ( 'Table1', 'Table2' ),
        "DateDiff", DATEDIFF ( 'Table1'[Date1], 'Table2'[Date2], DAY )
    )
)

image.png





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

Proud to be a Super User!







Power BI Blog

@GilbertQ,

Thank you!!!

Pleasusre to assist





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

Proud to be a Super User!







Power BI Blog

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.