How do I create a quarter-over-quarter variance column or measure with a table of account balance totals by quarter? - powerbi

I have a table that lists quarter-end account balances by account and business group and I'd like to create a measure or column that shows the quarter-over-quarter balance change for each company/account combo (e.g., Group A, Account 10000 quarter-over-quarter change is $500).
I've tried various formulas using the calculate function, but these don't work if I don't apply aggregation (SUM and SUMX are what I've tried) to the AMOUNT column. The aggregations seem to mess up the results. I either get blank values or duplicate amount values. I think it's because the amounts don't need to be aggregated. They're already aggregated values.
How can I do a simple subtraction based on quarterly dates without having to aggregate my amount column?
The formula(and similar variations) I've tried:
QtrChange =
VAR CurrentAmt = TABLE1[AMOUNT]
VAR PrevAmt = CALCULATE(SUM(TABLE1[AMOUNT]), PREVIOUSQUARTER(TABLE1[DATE].[DATE])
RETURN = CurrentAmt - PrevAmt
Table example:
Business Group
Account
Quarter end balance
Quarter
A
100000
$2000
3/31/22
A
200000
$3000
3/31/22
B
100000
$4000
3/31/22
B
200000
$5000
3/31/22
Edit:
Edited DAX formula with labels tying to example table, for better clarity:
QtrChange =
VAR CurrentAmt = TABLE1[Quarter end balance]
VAR PrevAmt = CALCULATE(SUM(TABLE1[Quarter end balance]), PREVIOUSQUARTER(TABLE1[Quarter].[DATE])
RETURN = CurrentAmt - PrevAmt

Related

How to have the table total the sum of my measure which has an IF condition

I'm creating a table that automatically calculates the bonus for each salesperson based off their sales figures. I'm struggling with the measure
Whale Accounts =
IF('Oct-Dec Refresh 1.5.23'[YearOverYear Variance] >
800000 && 'Oct-Dec Refresh 1.5.23'[Percentage Difference] >= 1.1, 2500, 0)
Any account that sold over $800,000 and had a sales growth increase of 10%, receives $2500 in bonuses. I created the measure and it shows $2,500 for each account that meets this criteria, but the table doesn't sum it, how can I get the table to sum the total?
Also, both YearOverYear Variance and Percentage Difference are measures. I have attached an image for clarification
I tried creating custom columns, using SUM, CALCULATE but I haven't been able to figure it out.
You need a summing iterator over your accounts for this. Something along the lines of:
Bonus =
SUMX (
VALUES ( 'Table'[Master Bill To] ) ,
[Whale Accounts]
)
Where the account column is the one you are using in the visualization!

PowerBI DAX Query - undo filter slicer on certain values based on expression

I have below table structure:
enter image description here
here I want to put a date slicer in Power BI to filter on dates and return the count of total rows as total in the card as shown below:
enter image description here
simple, the only twist is that I want to have the total of hybrid car added at all times.
i.e.
Brands before 5/25/2020 = 4 Hybrid + 1 Electric = 5
Brands before 12/5/2020 = 4 Hybrid + 3 Electric = 7
I have found a solution, which is creating a view in my database, which jus holds the number count of hybrid car (select count(*) from table where cartype = 'hybrid') and using it to sum with filter rows in power bi - but I am looking for a solution completely in Power BI DAX query.
any measure I have tried to create in power bi is filtered by date slicer and so doesn't work.
Create these measure:
TOTALROWS = COUNT('cars'[brand])
ELECTRIC_NUM = CALCULATE([TotalRows],('cars'),'cars'[cartype]="ELECTRIC")
HYBRID_NUM = CALCULATE([TOTALROWS],ALL('cars'),'cars'[cartype]="HYBRID")
TOTALBYBUSINESSLOGIC = CALCULATE([ELECTRIC_NUM]+[HYBRID_NUM])
Now use the last measure (i.e. TOTALBYBUSINESSLOGIC) to be used in your Card to display the total, Notice the expression diffrence between ELECTRIC_NUM and HYBRID_NUM
(In HYBRID_NUM I have used ALL, All will have it bypass the Date Slicer filter) whereas ELECTRIC_NUM will only proivde sum of rows falling in the active date sliver range.

Is there a way to let the user choose the columns in a dax groupby formula in Power BI?

I have a table with spending information per week, gender and age group.
Sample Table = {
(1,"Man","0-10",9),
(1,"Woman","0-10",10),
(1,"Man","10-30",8),
(1,"Woman","10-30",2),
(1,"Man","30-60",4),
(1,"Woman","30-60",6),
(1,"Man","60+",9),
(1,"Woman","60+",8),
(2,"Man","0-10",6),
(2,"Woman","0-10",4),
(2,"Man","10-30",8),
(2,"Woman","10-30",8),
(2,"Man","30-60",7),
(2,"Woman","30-60",10),
(2,"Man","60+",3),
(2,"Woman","60+",8),
(3,"Man","0-10",3),
(3,"Woman","0-10",10),
(3,"Man","10-30",3),
(3,"Woman","10-30",10),
(3,"Man","30-60",1),
(3,"Woman","30-60",9),
(3,"Man","60+",8),
(3,"Woman","60+",5)
}
Using this table, I can calculate the amount of spending per group. A 'group' can be defined in different ways: 'Men' (only gender) or 'Men in age group <60' (gender + age group) or 'Men in age group <60 in week 25' (gender + age group + weeknumber) etc. To calculate the amount spent per group (in this case, groupby per gender+weeknumber), I make a new table use the following dax codes:
grouped spending =
VAR spending =
GROUPBY('Sample Table',
'Sample Table'[Weeknumber],
'Sample Table'[Gender],
"sum_spent_group",
SUMX(CURRENTGROUP(), 'Sample Table'[spent]))
RETURN
spending
And add a column to this table to calculate the part spent by the chosen group over the total amount spent:
spending part =
DIVIDE(CALCULATE(SUM('grouped spending'[sum_spent_group]),
ALLEXCEPT('grouped spending','grouped spending'[group])),
SUM('grouped spending'[sum_spent_group])
)
Now I can show the spending percentage of a group over the total amount of spending:
Is there a way I can let the user choose (using buttons/bookmarks/etc..?) choose which columns should be in the groupby? In other words; is it possible to make this code more general, and let the user specify the group combination? As well as for the numerator as denomerator.
I realize one solution is to make columns for all group combinations, but in my real data, I have many more categories, so that will take a lot of space (and a lot of work..).
Thanks a lot!
If I understand you correctly, you don't actually need to create a separate table for each group.
Create a measure to calculate sum of spent that will be influenced by slicers (your group). Something like spent_calc = SUM('Sample Table'[spent])
Create a measure to calculate total spent regardless of slicers selected CALCULATE(SUM('Sample Table'[spent]),ALL('Sample Table'))
Create measure that calculates percentage of total spent %_spent_by_selected_group = DIVIDE([spent_calc],[total_spent])
Create slicers and put percentage measure into a desired visual, e.g.
Now a user can create any combination of attributes and see the percentage of the total spent. The caveat is of course you will have to have a lot of slicers if you have a lot of categories, so it might not be the most elegant solution. Probably the easiest one though.

How do I manipulate measure values based on 2 other dimension tables

Power BI newbie here and I'm trying to figure how to craft my DAX to manipulate my measure values based on certain criteria in the other two tables.
Currently I have 2 separate tables which are joined by a One to Many relationship and a separate Measures table. (Total Sales Price is computed as sum of Sales Price)
My aim is to create a new measure where Total Sales Price is multiplied by 1.5x when DIM_Product_Type[Product Category] = "High".
New Measure =
CALCULATE (
SUM ( FACT_PriceDetails[Sales Price] ),
FILTER ( DIM_Product_Type, DIM_Product_Type[Product Category] = "High" )
) * 1.5
However this returns no values in my visual and I'm trying to discern if its a matter of the table joins or the DAX expressions.
Thank you for your time!
Your measure seems good.
It will select only those products with a Product Category of "High" and multiply them by 1.5 to give you result. i.e. Give me the sum of all "High" Product category Price details multiplied by 1.5.
What you need to check is:
Product Serial Numbers match across the two tables
Your Product Category does indeed contain the category "High"
You have entries in FACT_PriceDetails that link to a DIM_Product_Type that has a category of "High"
Check you have not set any filters that could be hijacking your results (e.g. excluding the "High" product category product type or the realated fact/s)
Option-1
You can do some Transformation in Power Query Editor to create a new column new sales price with applying conditions as stated below-
First, Merge you Dim and Fact table and bring the Product Category value to your Fact table as below-
You have Product Category value in each row after expanding the Table after merge. Now create a custom column as shown below-
Finally, you can go to your report and create your Total Sales measure using the new column new sales price
Option-2
You can also archive the same using DAX as stated below-
First, create a Custom Column as below-
sales amount new =
if(
RELATED(dim_product_type[product category]) = "High",
fact_pricedetails[sales price] * 1.5,
fact_pricedetails[sales price]
)
Now create your Total Sales Amount measure as below-
total_sales_amount = SUM(fact_pricedetails[sales amount new])
For both above case, you will get the same output.

How can I calculate a monthly average including months with no records in Power BI

I'm trying to calculate a monthly average number of cases for each investigator. It might be over a quarter, year, or multiple years so it needs to respond to the visual or table context I drop it into. The base table has Case (individual case#), Investigator (person name), Date assigned (not shown), and from that date,month and year columns extracted and a YearMonth categorical column.
I create a caseCount measure as
caseCount = COUNT('Table'[Case])
I've tried several different ways to calculate the average over all months (in this case 4). Because Mary has cases in each month, her average is correct (1.75) but Sam's uses a denominator = 3, thus doesn't calculate correctly. (returns 1.3 instead of 1). How can I force the calculation to use the full number of months.
Additional notes:
There may be cases in the table that fall outside the date range I want so I've tried using a
Avg = CALCULATE(AVERAGE(caseCount), Table[Date] > #10/31/2019#)
I've also tried several variations using CALCULATE(DIVIDE(), [Date] > 10/31/2019. Everything seems to exclude those months when an investigator had no investigations assigned. I also tried connecting to a Date table and using the Distinct YearMonth value created there.
This is because the evaluation context.
I would define the measure as follow:
VAR _casecount = //count number of cases in the selected period, applied on the fact table
VAR _months = COUNTROWS(CALCULATETABLE(VALUES('Calendar'[Month]), ALLSELECTED('Calendar'))) //count number of months in the selected period
RETURN
_casecount/months
Update
I did not consider the scenario of multi-year periods involving May-2019 and May-2020. Then, let's reframe the solution using DATEDIFF:
VAR _casecount = //count number of cases in the selected period, applied on the fact table
VAR _firstCalendarDate = CALCULATE(MIN('Calendar'[Date], ALLSELECTED('Calendar'))
VAR _lastCalendarDate = CALCULATE(MAX('Calendar'[Date], ALLSELECTED('Calendar'))
VAR _months = DATEDIFF(_firstCalendarDate, _lastCalendarDate, MONTH)
RETURN
_casecount/months