In a previous post about earned premium, I outlined how you could calculate a monetary value based on a period of time over which it was earned using DAX.
Serendipitously, the next day a colleague forwarded Alberto Ferrari's paper on understanding DAX query plans, and after giving it a thorough read I fired up the query profiler and set out to optimize our calculated measure for earned premium.
Alberto's paper details a performant solution to the classic events in progress problem, of which earned premium is a close cousin. My excitement at lazily shoplifting Alberto's work came to a grinding halt when I discovered that his 40ms Jedi solution only worked if data was queried at a specific granularity. This wasn't going to cut it... we need an earned premium measure that works at any level of aggregation. Back to the drawing board.
It turns out that much of Alberto's advice is (as always) really valuable. While I strongly recommend reading Alberto's paper, here's the cheat sheet for optimizing any DAX calculated measure:
- Help the Formula Engine (FE) to push the heavy lifting down to the Storage Engine (SE).
FE is single threaded and non-caching, whereas SE is multithreaded and can cache results.
- Avoid complex / inequality predicates that cause SE to call back to the FE.
This not only slows down data retrieval, but also prevents SE from caching results.
Our original measure used inequality predicates in the expression:
'Premium'[Start Date] <= LASTDATE('Date'[Date])
&& 'Premium'[End Date] >= FIRSTDATE('Date'[Date])
&& 'Premium'[End Date] >= FIRSTDATE('Date'[Date])
... which forces SE to callback to FE, slowing down the calculation somewhat.
To recap:
Earned Premium = Amount Paid * Days In Current Period / Total Days of Cover
Earned Premium = Amount Paid * Days In Current Period / Total Days of Cover
Which we can calculate as follows:
Earned Premium:=
SUMX (
SUMMARIZE (
'Premium',
'Premium'[Start Date],
'Premium'[End Date],
"Earned Premium",
SUM ('Premium'[Amount Paid] )
* COUNTROWS (
CALCULATETABLE ('Date',
KEEPFILTERS(
DATESBETWEEN (
'Date'[Date],'Premium'[Start Date], 'Premium'[End Date]
)
)
)
)
/ COUNTROWS (
DATESBETWEEN (
'Date'[Date], 'Premium'[Start Date], 'Premium'[End Date]
)
)
),
[Earned Premium]
)
With this formula, we're able to calculate earned premium for 22 million records across 84 months in 2.2 seconds. Happy days.
To explain the calculation:
- The SUMMARIZE function groups all premium by start & end date.
- We project "Earned Premium" as the sum of all premium for a distinct start & end period
- The earned premium is multiplied by the number of days (records) in the distinct period that are also present in period currently being observed in the resulting cell. KEEPFILTERS applies this filtering for us - it effectively says "filter out days that aren't in the current month / quarter / year filtering being applied to the Date table"
- The result is divided by the number of days occurring in the distinct period regardless of filtering.
- Finally SUMX adds up all of the summarized values.
Comments
Can you modify your script to pull and read zip file from web ?