Our Subscription table is contains around 40k entries. I am trying to calculate the Monthly-Recurring.Dim is a simple calendar table to map the date ranges start and end to the days.
Sumx('Subscription Clean',
CALCULATE(
SUMX(
SUMMARIZE(
FILTER(
CROSSJOIN(
'Subscription Clean',Dim
),
Dim[Date] <= 'Subscription Clean'[End At] &&
Dim[Date] > 'Subscription Clean'[Start At]
),
'Subscription Clean'[Duration total],
'Subscription Clean'[Duration],
'Subscription Clean'[End At],
'Subscription Clean'[Start At],
'Subscription Clean'[Price]),
DIVIDE(DIVIDE(
'Subscription Clean'[Price],DATEDIFF([Start At],[End At],DAY)+1
),1.19) % Tax of 19
)
)
)
The data looks like this
Sales Id
Product name
created At
end at
Cancelled At
Price in €
Duration
1
bananas
01.01.2021
01.03.2021
28.02.2021
20.00
2
2
apples
01.02.2021
01.05.2021
null
90€
3
The output should be like this for the mmr each month 10€ come from the first position for the months {1,2}
Each price will be distributed between the dates Start and End.
jan = 10
feb = 40
mar = 30
apr = 30
may = 0
The DAX query is working properly but hell a slow. Is there any way to improve the performance? Further the calculated measure will be used to do more calculations and needed to be loaded in memory if possible for quick access. Maybe anyone knows how to do it properly and I can leech some good hints ;)
Related
I have created a report for the HR department. One of the visuals aims to display the pyramid of ages of the employees by gender (we take cluster of 5 years, e.g. 20-25 for people between 20 and 25 years old).
To make it simple, data wise, I have a table with the list of employees, including their date of birth and many other fields, not relevant for this post. I added a calculated column with the age cluster based on the today’s date:
AgeCluster =
VAR AgeCalc=if(HR_DATA[Birthdate]=blank(),blank(),DATEDIFF(HR_DATA[Birthdate],today(),YEAR))
VAR Mult5=INT(AgeCalc/5)
RETURN
if(isblank(AgeCalc),blank(),5*Mult5&"-"&5*(Mult5+1))
And I have a basic visual (tornado chart with the AgeCluster in Group, showing male and female)
Now my issue is that my report should be dynamic, so the user should be able to see the situation in the past or in the future... I have a calendar table (not linked to my HR_Data table), and a date slicer on my report's page. I need the age cluster to be recalculated.
I have tried a calculated table, but I can’t get it working properly. I have read various blog posts on similar issue, but still can't figure out how to solve it...
Any idea or tips much appreciated.
Thank you so much!
Dynamic filtering usually means in these situations that you need a fixed x-axis to work with. That x-axis is calculated outside the original table, such as a parameter table.
Assuming your data looks like this:
Table: Employees
EmployeeID
DOB
1
20 March 1977
2
05 December 1981
3
25 December 1951
4
20 December 1954
5
04 March 1980
6
24 July 1968
7
07 June 1984
8
01 October 1992
9
25 February 1999
10
02 November 1987
First, we need to create the Age Groups with their respective lower and top bands. This table uses a similar code when a parameter table is created.
Table: Buckets
Because you are using whole numbers, I think is best to calculate the TopBand by adding 4. In that case, you don't have repeating numbers. In other words, ranges shouldn't overlap (20-25 and 25-30)
Buckets =
SELECTCOLUMNS (
GENERATESERIES ( 20, 80, 5 ),
"Age Series",
[Value] & " - " & [Value] + 4,
"LowerBand", [Value],
"TopBand", [Value] + 4
)
Also, we will need a Calendar Table to filter accordingly.
Table: CalendarHR
CalendarHR =
ADDCOLUMNS (
CALENDAR ( MIN ( Employees[DOB] ), TODAY () ),
"Year", YEAR ( [Date] )
)
With all that, you can create a calculation that can be filter dynamically.
DAX Measure:
EmployeesbyBracket =
VAR _SelectedDate =
MAX ( CalendarHR[Date] )
VAR _SelectedLowerBand =
SELECTEDVALUE ( Buckets[LowerBand] )
VAR _SelectedTopBand =
SELECTEDVALUE ( Buckets[TopBand] )
VAR EmployeesAge =
ADDCOLUMNS (
Employees,
"Age",
INT ( DATEDIFF ( [DOB], _SelectedDate, YEAR ) / 5 ) * 5
)
RETURN
COUNTROWS (
FILTER (
EmployeesAge,
[Age] >= _SelectedLowerBand
&& [Age] <= _SelectedTopBand
)
)
Output
I've created a table visual with the Age Groups from the Buckets table.
Both a calculated column and a calculated table will be static and not achieve your desired objective. You need age to be calculated via a measure.
Create a disconnected table with your age buckets and then implement the dynamic segmentation pattern.
I am struggling to get my measure to SUM correctly.
In general if revenue is blank fill the blank with Budget number.
My formula so far:
Revenue = IF(ISBLANK([TotalRevenue]),[TotalBudget],[TotalRevenue])
I have tried this with a column but it wont work because my budgeted results are from a different table vs where my revenue is.
Also tried with
HASONEFILTER -
Measure =
VAR TotalRevenue = SUM(Paysuite[NET_REVENUE])
VAR TotalBudget = SUM(Budget[Bgt Revenue])
RETURN
IF(HASONEFILTER('Date'[Fiscal Year]),IF(ISBLANK([TotalRevenue]),[TotalBudget],[TotalRevenue]),SUMX(FILTER(Budget,Budget[TotalBudget]),[TotalBudget]))
example
JAN - 1
FEB - 2
MAR - 3
APR (bgt) - 1
MAY (bgt) - 1
Total comes to 6 where expected result should be 8
thanks in advance
worked it out - I had the wrong field in hasonefilter - should be something that we wont use as a filter at all
Measure =
IF(
HASONEFILTER(P[EZ.Count]),
IF(
ISBLANK([TotalRevenue]),
[TotalBudget],
[TotalRevenue]
),
SUMX(
VALUES(Budget[Bgt Revenue]),
[Revenue]
)
)
I have a yearly goal for number of graduates. I want to distribute this yearly number to monthly level using predefined percent numbers.
I would like to get
jan = 1.7% * 292 = 4.96
feb = 1.4% * 292 = 4.01
etc...
The problem is that yearly number of graduates has date of 2021-01-01 and relation to date table, so it will only work for the first month. (Other months are blank). I cannot change the date relation because I have other goals in the same table that use month
Here are my measures
Graduates goal = CALCULATE( SUM(value), Measure = 'Graduates goal')
Goal% = CALCULATE( SUM(value), Measure = 'Graduates%)
Montly graduate target = CALCULATE( [Graduates goal] * [Goal%])
I have tried using ALL(Dates[Month]) ALL(Dates[Year]) but I cannot get past that month level restriction in yearly goal.
Update:
I was able to solve this with crossfilter something like this
Montly graduate target = CALCULATE([Graduates goal], CROSSFILTER( Goals, Dates, None), YEAR(Pvm) = YEAR(TODAY())) * Goal%
Using Power Bi desktop,
I need to create a query where the result will be the current month's working days.
For example July 2021 has 22 working days and so on.
My goal for achieving this will be to be to average the number of lines processed divided into the number of working days for the current month.
Will this be possible?
You can always get the job done only by creating just a single measure and nothing else like following
_count =
COUNTX (
FILTER (
VALUES('fact'[_factDate]),
WEEKDAY ( 'fact'[_factDate], 1 ) <> 1
&& WEEKDAY ( 'fact'[_factDate], 1 ) <> 7
),
'fact'[_factDate]
)
The minimum dependency of the calculations are having a calendar table like this
Calendar_Date
Calendar_Year
Calendar_Month
1/1/2000
2000
1
first create a calculated column in you calendar table (Dates for my case) with this below code-
Column = if(
FORMAT(Dates[date],"dddd") = "Saturday"
|| FORMAT(Dates[date],"dddd") = "Sunday",
0,
1
)
now create a measure as below-
weekday = sum(Dates[Column])
now, create visual with month and new measure.
I've been struggling with this and hope someone is able to help...
I have a table with sales for products over multiple years. There is a measure that gives me the total revenue for each year by customer, ignoring product sold:
totalRevenueMeasure =
CALCULATE (
SUM ( test[Revenue] ),
ALLEXCEPT ( test, test[company], test[year] )
)
Year company Product revenue totalRevenueMeasure rankx (revenue in year)
2018 company a shoes 100 300 1
2018 company a mugs 200 300 1
2018 Company b shoes 250 250 2
2019 company a lamps 300 300 2
2019 Company b shoes 350 450 1
2019 Company b mugs 100 450 1
2019 Company c mugs 100 100 3
2020 company a shoes 150 150 2
2020 Company c lamps 200 200 1
The closest I got to the RANKX measure is below but this doesn't give the correct results. The expected output is in the RANKX column of the table above.
Customer Rank =
RANKX(
ALLSELECTED( test[company],test[year]),
[TotalRevenueMeasure],
,
DESC,
Dense
)
Thanks in advance for pointers, DAX is still eluding me a bit and there might be a better way to go about it.
Following the recommendation from Alexis, success with test data but live skips some rows in rank - year 2019 doesn't have a rank #1 but has 2 rank #2. I guess this must be some kind of data issue...
You're very close. The problem is that you are looking to rank each year separately but you've removed the Year filter context with your ALLSELECTED function.
Take out the second argument in ALLSELECTED so that you only have company (since you don't actually want to rank over all years for each row).