PowerBi - Sum measure - powerbi

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]
)
)

Related

PowerBI / DAX - Sum for last days of month per each category - how to simplify

I have a quite peculiar problem. I have a column with values that represents the state of Inventory for each Site (category).
Which means that the most recent one value for each month is always last day per each site per month.
Example
for site 667 for november its going to be value 5 252 235.74 (31/12/2021) but for site 200 its going to be 79 967 894.18 (30/12/2021)
he sum of those values should be 85 220 129.92 which is state of inventory for those two sites per december.
I was able to calculate this with this measure
Inventory Cost =
VAR _pretable =
ADDCOLUMNS (
SUMMARIZE (
v_factinventorytransactions,
v_dimdate[DateId],
v_factinventorytransactions[SiteId]
),
"InventoryCost", CALCULATE ( AVERAGE ( v_factinventorytransactions[RunningCost] ) )
)
VAR _table =
FILTER (
_pretable,
VAR _MaxDate =
CALCULATE (
MAX ( v_factinventorytransactions[InventoryTransactionDateId] ),
ALLSELECTED ( v_dimdate[DateId] )
)
RETURN
v_dimdate[DateId] = _MaxDate
)
RETURN
SUMX ( _table, [InventoryCost] )
Which works perfectly but I'm wondering if it can be simplyfied. I want it to simplify, because when I want to use this measure inside another one that sums those Inventory Cost values per month for last 3 months and I have wrong answers.
Which means that this Inventory Cost measure works but if I call out this measure in the one below it shows wrong numbers (but other measures, more simply ones work).
Rolling3Months =
VAR _EndDate = MAX(v_dimdate[Date])
VAR _Dates = DATESINPERIOD(v_dimdate[Date], _EndDate, -3, MONTH)
VAR _Cost = [Inventory Cost]
VAR _Inventory = SUMX(_Dates, CALCULATE(_Cost, ALL(v_dimdate[YearMonth])))
RETURN
_Inventory
I'm a little bit stuck and would be super appreciated when someone would pointed out my mistakes/errors here.
I'm also providing sample power BI file with those.
https://wetransfer.com/downloads/cabd5902e1491b6874064a0deb26d0ae20220614185839/dac3e16acdce4c2b707e92bd56a04d1020220614185904/77a258
Thank you

Calculate age cluster dynamically in DAX - Power BI

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.

Power Bi Super slow Query needs a performance boost

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 ;)

Calculate Number of Working Days based on a Month

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.

Power BI Rolling Total Previous Month DAX

I am working in POWER BI and trying to calculate a DAX expression for the rolling total of the previous month. I have a filter where I select a certain month, I would like to calculate the rolling total for the previous month.
Below is the calculation that works perfectly to calculate the rolling total for the selected date range.
How can I calculate the previous months rolling total?
Rolling_Total_Current_Month = CALCULATE(
SUM(SalesInvoice[Sales])
,FILTER(ALLSELECTED(SalesInvoice), (SalesInvoice[Date]) <= MAX(SalesInvoice[Date])))
Here is a sample of my data, I have sales per day, for multiple categories, (in fact i have a couple more columns of details but this is simplified)
Date Day Amount Category
1/1/2016 1 100 A
1/1/2016 1 120 B
1/1/2016 1 90 C
1/2/2016 2 500 A
1/2/2016 2 321 B
1/2/2016 2 143 C
So far I have come up with an equation to solve the rolling total, but when I try to slice is and view the rolling total of a single category it does not work for the previous month. I just keeps the original rolling total for the previous month.
Here is the equation for the rolling total previous month that works. But does not recalculate a rolling total for the previous month once sliced based on category.
PREVIOUS_MONTH_ROLLING_TOTAL =
CALCULATE(
[Current Sales]
,FILTER(
ALL( Orders )
,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
&& Orders[Day] <= MAX( Orders[Day] )
)
)
I have solved how to get the previous months rolling total.
You must do three things. First create a Month Number column in your data sheet (this is used as an integer to subtract 1 month from). You must also create a days column as well.
Then create a measure for Current Sales or whatever your value is.
Create a measure for the current month sales
Current Sales = SUM(Orders[Amount])
Then this equation.
PREVIOUS_MONTH_ROLLING_TOTAL =
CALCULATE(
[Current Sales]
,FILTER(
ALL( Orders )
,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
&& Orders[Day] <= MAX( Orders[Day] )
)
)
The Idea of this equation is to be able to display the previous months rolling total on a chart with the X axis as "DAY" (so 1-31) Then you can view the current month, previous month, same period last year all on the same chart or table.
Try something along these lines:
Rolling_Total_Previous_Month =
VAR CurrentMonth = MAX(SalesInvoice[Date])
VAR PreviousMonth = EOMONTH(CurrentMonth,-1)
RETURN CALCULATE(SUM(SalesInvoice[Sales]), SalesInvoice[Date] <= PreviousMonth)
To start of, I have a data like this in a table called as Orders-
Date Amount
12/12/2017 100
12/12/2017 200
12/12/2017 300
1/1/2018 400
1/1/2018 500
I first create a calculated column called as Year & Month by using the formula:-
Year = YEAR(Orders[Date])
Month = FORMAT(Orders[Date],"mmmm")
Then I create a column called as Month Number which will be helpful for sorting when more than one year is involved in the table and as well as to Identify the Previous Months.
MonthNumber = DATEDIFF(Min(Orders[Date]),Orders[Date],MONTH)
Current Month Sales can be a measure or a Calculated column. Qn the Question, you had your answer for current month sales via a calculated column and if you want to go for a measure then something like this would work on a summary table.
Current Month Sales = SUm(Orders[Amount])
I would also create a column called as Key:-
Key = Orders[MonthNumber] & Orders[Category]
Now, for the Previous Month Sales, I would create a measure that looks for selected MonthNumber that we created.
Previous Month Sales =
Var SelectedCategory = SELECTEDVALUE(Orders[Category])
Var SelectedMonthNumberr = SELECTEDVALUE(Orders[MonthNumber]) - 1
Var ReqKey = SelectedMonthNumberr & SelectedCategory
Return
IF(ISBLANK(SelectedCategory) <> True(),
CALCULATE(SUM(Orders[Amount]),FILTER(ALL(Orders), Orders[Key] = ReqKey)),
CALCULATE(SUM(Orders[Amount]),FILTER(ALL(Orders), Orders[MonthNumber] = SelectedMonthNumberr)))
or to your Measure
PREVIOUS_MONTH_ROLLING_TOTAL =
CALCULATE(
[Current Sales]
,FILTER(
ALL( Orders )
,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
&& Orders[Day] <= MAX( Orders[Day] )
)
)
You can just add another filtering item as && Orders[Category] = SELECTEDVALUE(Orders[Category]) but won't work when you don't have any categories selected or on a table or visual which doesn't have categories. So, you would need to define an if else logic here as I have quoted on my measure formula.
Do let me know, if this helps or not.