Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreThe FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now
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:
| ID1 | Date1 |
| 1 | 05-12-18 |
| 2 | 04-12-18 |
| 3 | 04-12-18 |
| 4 | 04-12-18 |
Table2:
| ID2 | Date2 |
| 1 | 20-11-18 |
| 2 | 27-11-18 |
| 3 | 04-12-18 |
| 4 | 11-12-18 |
A new table:
| ID1 | Date1 | ID2 | Date2 | Diff |
| 1 | 05-12-18 | 1 | 20-11-18 | 15 |
| 1 | 05-12-18 | 2 | 27-11-18 | 8 |
| 1 | 05-12-18 | 3 | 04-12-18 | 1 |
| 1 | 05-12-18 | 4 | 11-12-18 | -6 |
| 2 | 04-12-18 | 1 | 20-11-18 | 14 |
| 2 | 04-12-18 | 2 | 27-11-18 | 7 |
| 2 | 04-12-18 | 3 | 04-12-18 | 0 |
| 2 | 04-12-18 | 4 | 11-12-18 | -7 |
| 3 | 04-12-18 | 1 | 20-11-18 | 14 |
| 3 | 04-12-18 | 2 | 27-11-18 | 7 |
| 3 | 04-12-18 | 3 | 04-12-18 | 0 |
| 3 | 04-12-18 | 4 | 11-12-18 | -7 |
| 4 | 04-12-18 | 1 | 20-11-18 | 14 |
| 4 | 04-12-18 | 2 | 27-11-18 | 7 |
| 4 | 04-12-18 | 3 | 04-12-18 | 0 |
| 4 | 05-12-18 | 4 | 11-12-18 | -6 |
Solved! Go to Solution.
Hi there
Here is the DAX code
Table = CROSSJOIN('Table1','Table2')
This will give you the result you want
Here we go
Table =
CALCULATETABLE (
ADDCOLUMNS (
CROSSJOIN ( 'Table1', 'Table2' ),
"DateDiff", DATEDIFF ( 'Table1'[Date1], 'Table2'[Date2], DAY )
)
)
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.
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.
You are welcome.
Hi there
Here is the DAX code
Table = CROSSJOIN('Table1','Table2')
This will give you the result you want
Hi there
You should be able to do it with a DATEDIFF(StartDate,EndDate,INTERVAL)
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 )
)
)
Pleasusre to assist
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 52 | |
| 38 | |
| 37 | |
| 19 | |
| 18 |
| User | Count |
|---|---|
| 67 | |
| 67 | |
| 34 | |
| 32 | |
| 29 |