15.12.2020 Views

Beginning DAX with Power BI_ The SQL Pro’s Guide to Better Business Intelligence ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5

Joins

Table 5-11. Output of NATURALLEFTOUTERJOIN from Listing 5-12

Customer Purchase Date Last Purchase

1 2019-01-01

1 2019-01-07 2019-01-01

1 2019-01-21 2019-01-07

1 2019-01-25 2019-01-21

2 2019-01-05

2 2019-01-12 2019-01-05

2 2019-01-17 2019-01-12

2 2019-01-22 2019-01-17

The final requirement is to have a column that carries a number that shows, for

each customer purchase, how many days have passed since their last purchase. This is

achieved by wrapping the NATURALLEFTOUTERJOIN function with an ADDCOLUMNS

function (Listing 5-15).

Listing 5-15. The Final RETURN Statement from Listing 5-12

RETURN

ADDCOLUMNS(

NATURALLEFTOUTERJOIN('Sales', LastPurchases),

"Days since last purchase",

VAR DaysSince = INT([Purchase Date] -

[Last Purchase])

RETURN IF (

NOT ISBLANK([Last Purchase]),

DaysSince

)

)

The ADDCOLUMNS function appends a single column to the output of the

NATURALLEFTOUTERJOIN function. The “Days since last purchase” parameter

provides a name for the new column, while the last parameter starting with a nested VAR

statement is a DAX expression that returns the difference between the [Purchase Date]

and [Last Purchase] columns.

116

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!