Circular Dependency Calculated Column Power BI Desktop - powerbi

In my Calculated Table, variable AsOfDate should be reflected by slicer dim_Date which is also calculated table.
Calculated table CrossTableEffectiveDate derived from table fact_Premium that have Date relationship to dim_Date.
I got error: Circular Dependency Calculated Column
CrossTableEffectiveDate =
VAR AsOfDate = VALUE("2019-01-31") //This value should be based on date value in a slicer
VAR CrossTables =
CROSSJOIN(
SUMMARIZE(fact_Premium,
fact_Premium[PolicyNumber],
fact_Premium[CompanyLocationGuid],
fact_Premium[Coverage],
fact_Premium[State],
fact_Premium[SICCode],
fact_Premium[ASLOB],
fact_Premium[ProducerGUID],
"Start", MIN(fact_Premium[EffectiveDate]),
"End", MAX(fact_Premium[ExpirationDate]),
"Premium", SUM(fact_Premium[Premium])
),
'Calendar')
VAR RiskPeriods =
ADDCOLUMNS(
FILTER(CrossTables,
'Calendar'[EoMonth] >= [Start] && 'Calendar'[Month] <= [End] && 'Calendar'[Month] <= AsOfDate),
"StartRiskMonth", IF([Start] > 'Calendar'[Month], [Start], 'Calendar'[Month]),
"EndRiskMonth", IF([End] < 'Calendar'[EoMonth], [End], 'Calendar'[EoMonth])
)
RETURN SELECTCOLUMNS(RiskPeriods,
"PolicyNumber", fact_Premium[PolicyNumber],
"CompanyLocationGUID", fact_Premium[CompanyLocationGuid],
"Coverage",fact_Premium[Coverage],
"State", fact_Premium[State],
"SICCode",fact_Premium[SICCode],
"ASLOB", fact_Premium[ASLOB],
"ProducerGUID",fact_Premium[ProducerGUID],
"StartRiskMonth", [StartRiskMonth],
"EndRiskMonth", [EndRiskMonth],
"YearNum", YEAR('Calendar'[Month]),
"Qtr", ROUNDUP(MONTH('Calendar'[Month])/3, 0),
"MonthNum", MONTH('Calendar'[Month]),
"WrittenPremium", [Premium],
"DaysInMonth", [EndRiskMonth] - [StartRiskMonth] + 1,
//"EndRiskMonth-Start", [EndRiskMonth] - [StartRiskMonth] + 1,
//"End-Start",[End] - [Start] + 1,
"EarnedPremium", [Premium] *
DIVIDE([EndRiskMonth] - [StartRiskMonth] + 1, [End] - [Start] + 1))
How can I make AsOfDate variable be referenced by Date slicer from dim_Date?

You cannot have a calculated table or calculated column be dependent on a slicer.
These are only calculated once per data load and are not responsive to any filtering you have in your report.
You can add your support to this idea to improve the chances of Microsoft implementing it.
Dynamic calculated column

Related

DAX - SUMMARIZE based on SWITCH statement

I have SKUs data with some financial information. I have grouped the SKUs in 4 categories using a measure with SWITCH. Now I would like to summarise the information (e.g. total revenues of the products in each category) but I cannot get the summary to add up to the correct value.
Data model:
FactTable: fact table containing financial information (product code, revenue, profit, period, etc.)
DimSKU: mapping table with SKUs data (product code, brand, segment, etc.)
DimDates: date table (period, months, etc.)
Creating 4 product categories:
(using a measure and not a calculated column as the users can use a dropdown list and select different periods)
My Quadrants =
VAR
Quadrants =
SWITCH(
TRUE(),
AND([SKU_profit] >= [Total_profit], [SKU_growth] >= [Total_growth]), "Maintain",
AND([SKU_profit] < [Total_profit], [SKU_growth] < [Total_growth]), "Address",
AND([SKU_profit] >= [Total_profit], [SKU_growth] < [Total_growth]), "Grow",
AND([SKU_profit] < [Total_profit], [SKU_growth] >= [Total_growth]), "Investigate"
)
RETURN
Quadrants
Calculating YTD sales:
YTD revenue =
VAR
YTD_revenue = TOTALYTD(SUM(FactTable[Revenue]), DimDates[Date])
RETURN
YTD_revenue
Summarising data:
I can add the measure to a visual like a table and it works perfectly fine as long as I also display all the SKUs. Ideally, I would like a summary like the following:
MyQuadrants / Revenue
Maintain / 1,000
Address / 250
Grow / 500
Investigate / 350
I would be grateful if anyone could point me in the right direction.
Thanks all.
Edit
I have managed to make it work thanks to David's suggestion. See below for reference.
Creating configuration table
ConfigTable =
VAR
Total_profit = [Total_profit]
VAR
Total_growth = [Total_growth]
RETURN
{
("Maintain", Total_profit, 1, Total_growth, 1),
("Address", 0, Total_profit, 0, Total_growth),
("Grow", Total_profit, 1, 0, Total_growth),
("Investigate", 0, Total_profit, Total_growth, 1)
}
// 5 columns: quadrant, min profit, max profit, min growth, max growth
// Use table constructor to use variables
Creating measure
YTD revenue (segmented) =
CALCULATE(
[YTD revenue],
FILTER(
VALUES(DimSKU),
NOT ISEMPTY(
FILTER(
ConfigTable,
(
[SKU_profit] >= ConfigTable[min profit] &&
[SKU_profit] < ConfigTable[max profit] &&
[SKU_growth] >= ConfigTable[min growth] &&
[SKU_growth] < ConfigTable[min growth]
)
)
)
)

Power BI - Add slicer for thousands and millions

Does anyone know how you can add a power BI slicer so that the values in a matrix go from thousands to millions based on the selection of another slicer?
So if I select all business units then the sum of all values are returned in millions, else if i select one business unit, all values are returned in thousands?
Sorry, I have no DAX code.
Thanks.
This is covered nicely here:
https://www.daxpatterns.com/parameter-table/
(The DAX below is copied from that page.)
Create a new parameter table to use in a slicer.
Scale =
DATATABLE (
"Scale", STRING,
"Denominator", INTEGER,
{
{ "Units", 1 },
{ "Thousands", 1000 },
{ "Millions", 1000000 }
}
)
Incorporate the denominator into your measure(s). For example,
Sales Amount :=
VAR RealValue =
SUMX ( Sales, Sales[Quantity] * Sales[Net Price] )
VAR Denominator =
SELECTEDVALUE ( Scale[Denominator], 1 )
VAR Result =
DIVIDE ( RealValue, Denominator )
RETURN
Result

Sum row with next row value and grab next value in other column for the date selected

Let's say I have this data:
Earn Earn Cum.
13-Apr - -
14-Apr 48 48
15-Apr 257 305
16-Apr 518 823
17-Apr 489 1,312
18-Apr 837 2,149
19-Apr 1,005 3,154
20-Apr 1,021 4,175
21-Apr 1,463 5,638
22-Apr 2,630 8,268
23-Apr 2,993 11,261
24-Apr 3,354 14,615
25-Apr 4,332 18,947
26-Apr 4,885 23,832
27-Apr 4,514 28,346
28-Apr 4,356 32,702
29-Apr 4,824 37,526
30-Apr 7,082 44,608
1-May 6,091 50,699
2-May 1,407 52,106
When a date is selected in a dropdown slicer for example: 1-May I'd like to sum the rows 1-May with the next row 2-May for the column Earn and grab the next value 52,106 for the column Earn Cum.. The result should be:
1-May 7,498 52,106
Another example: if the date selected was 30-Apr the result must be:
30-Apr 13,173 50,699
I'm scratching my head trying to do this using a measure in Power BI.
I'll call your table "Data".
Create a measure:
Next Earn =
VAR Current_Date = MAX ( Data[Date] )
VAR Next_Date = Current_Date + 1
RETURN
CALCULATE (
SUM ( Data[Earn] ),
Data[Date] = Current_Date || Data[Date] = Next_Date
)
Create another measure:
Next Cum Earn =
VAR Current_Date = MAX ( Data[Date] )
VAR Next_Date = Current_Date + 1
RETURN
CALCULATE ( SUM ( Data[Earn Cum] ), Data[Date] = Next_Date )
Result:
Note: the code assumes that your dates are sequential (no gaps). If they have gaps, things are a bit more complex.
It will help to have access to date intelligence, so create a date table if you don't have one already. The following dax sets up a small table that just barely covers the sample data in your example.
Date = CALENDAR(Date(2019,4,10),Date(2019,5,5))
Create a relationship between the Dates in your table and the new Date dimension. Add a slicer to your visuals using the Date dimension.
We can use IF and ISFILTERED to check if filtering is being done. If we aren't filtering on Date then we get the normal table behavior. If we are, we'll see our modified result.
Earn _ Alt =
var tomorrow = CALCULATE (
SUM(Table1[Earn]),
dateadd('Table1'[Date], 1, DAY)
)
return Sum(Table1[Earn]) + IF(ISFILTERED`('Date'[Date]),tomorrow,0)`
and
Earn Cum. _ Alt = IF(ISFILTERED('Date'[Date]),
CALCULATE (
SUM(Table1[Earn Cum.]),
dateadd('Table1'[Date], 1, DAY)
),
SUM(Table1[Earn Cum.]))
Results:
-Unfiltered-
-Filtered-

Filter a column dynamically using a measure in Power BI

There is a column with FY i.e. "FY19","FY18","FY17" etc.
I Created a measure which calculates the current FY:
CurrFY =
CONCATENATE (
"FY",
IF (
MONTH ( TODAY () ) <= 3,
VALUE ( FORMAT ( TODAY (), "YY" ) ),
VALUE ( FORMAT ( TODAY (), "YY" ) ) + 1
)
)
e.g. output: "FY19"
Now i need to filter the report based on the FY column using the current FY i get from the CurrFY measure.
How do i do it?
Create a calculated column on your table using that measure
FYFilter = IF(Table1[FY] = [CurrFY], 1, 0)
Then add that column as a report level filter where FYFilter is 1.

How to break down date range by each month in DAX Power BI?

I have two dates:
'2018-01-05' and '2019-01-05'
How to create calculated table to break down those dates by month.
Should look simething like that:
There are probably many ways to do this, but here's one way that combines a few different concepts:
Table =
VAR Starting = DATE(2018, 1, 5)
VAR Ending = DATE(2019, 1, 5)
VAR MonthTable =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR(Starting, Ending),
"StartDate", EOMONTH([Date], 0) + 1),
[StartDate],
"EndDate", EOMONTH([StartDate], 0) + 1)
RETURN UNION(
ROW("StartDate", Starting, "EndDate", EOMONTH(Starting, 0) + 1),
FILTER(MonthTable, [EndDate] < Ending && [StartDate] > Starting),
ROW("StartDate", EOMONTH(Ending, -1) + 1, "EndDate", Ending)
)
Basically, you start with the CALENDAR function to get all the days, tag each date with its corresponding month, and then summarize that table to just return one row for each month.
Since the first and last rows are a bit irregular, I prepended and appending those to a filtered version of the summarized month table to get your desired table.
Create new table as
Table = CALENDAR( DATE(2018, 5, 1), DATE(2019, 1, 5) - 1)
Rename auto-generated column "Date" into "Start Date". Add new column as
End Date = Start Date + 1