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.
I have a dataset of patients visiting several categories(SPECIALISM) of a hospital. A visit lasts a couple of hours each day. Each row in my dataset represents an hour that they are present for a certain hospital specialism.
Input
I want to calculate for each hour of the day, the number of patients that are present on average, per specialism, I used the following code (measure):
daggem = AVERAGEX(values('Date'[Date]),[distinctpat])
with distinctpat being a distinct count of patient IDs
This gives me almost the desired result, but the tales of the graph are too heavy (it shows an average of 1 patient during the night, but this 1 patient was there only on 1 specific day, normally it is zero. But the average, as I calculated, it does not include all other nights when there were zero patients. So I would like to obtain an average that is much lower (and more correct)
Output
You probably need an extra table with all the days and times. Based on the reports, you will be able to find the hours without visits
Here is an example of how to create a Date table with hours; Add relationship and use in calculation.
DatesWithHours =
var Dates = CALENDAR("2021-01-01 00:00:00", "2021-01-03 00:00:00")
var DatesHour =
GENERATE(Dates, (ADDCOLUMNS(GENERATESERIES(1,12,1),"Hours", [Date] + TIME([Value],0,0))))
return
DatesHour
That's the problem with AVERAGEX, it ignores blanks. Try a simple division instead.
daggem =
DIVIDE (
COUNTROWS ( TargetTable ),
COUNTROWS ( Dates )
)
I need to count how many items/rows where created in the past months are.
general_report is the table name and [created] the date column format is in ormal dd/mm/yyyy hh:mm:ss as you can see here
For the goal, I have created this measure:
_created_last_month = COUNTROWS(FILTER(general_report, [Created] >= ( TODAY() - MONTH(TODAY() ) )))
I was also considering about creating a custom column which makes the same computation and then count how many items are valid in that column, with no success
Here are some examples (maybe you have to replace semicolon with comma)
Counts created in actual month:
_created_this_month:=
CALCULATE(COUNT(general_report[created]);DATESBETWEEN(general_report[Date];date(YEAR(TODAY());MONTH(TODAY());1);TODAY()))
Counts created in prev. month:
_created_last_month:=
CALCULATE(COUNT(general_report[created]);DATESBETWEEN(general_report[Date];date(YEAR(TODAY()-30);MONTH(TODAY()-30);1);EOMONTH(TODAY();-1)))
Counts created in the last "full" 6 months, the actual is not includet
_created_last_6_months_without_the_actual_month:=
CALCULATE(COUNT(general_report[created]);DATESINPERIOD(general_report[Date];EOMONTH(date(YEAR(TODAY()-30);MONTH(TODAY()-30);1);0);-6;MONTH))
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
I couldn't find an answer for my issue elsewhere, so trying my luck here.
I have sales table and my final result should determine if there were sales made for same person in specific period of time, for example within 7 business days. for example:
For ID 123 I have to flag it that sale for products A,B,C where within specified period.
For ID 1234 only sales of products A and B meet the criteria product C was sold in irrelevant time frame.
I've created a date table with indicators that determine for each date if the date is a working day, but i am struggling to calculate the relevant last working day
For example: I need that for 01/01/2019 i will get 10/01/2019 date, based on NUMOFDAYS and FinalWorkday = TRUE, which basically means that i have to count NUMOFDAYS times TRUE statement for each date and return corresponding date.
After that step done I think that it would be pretty easy for me to determine if the sale for a person was made in specific time frame.
If someone can give me direction for that much appreciated
Thank you in advance
You could use a DateTable like this one:
I used the following DAX-expressions for the calculated columns:
nrDays = 7
isWorkDay = WEEKDAY('DateTable'[Date],2) < 6
rankWorkingDays = RANKX ( FILTER ( DateTable, DateTable[isWorkDay] = TRUE () ),
DateTable[Date] , , ASC )
LastWorkDay = LOOKUPVALUE ( DateTable[Date],
DateTable[isWorkDay], TRUE (),
DateTable[rankWorkingDays], DateTable[rankWorkingDays] + DateTable[nrDays])
This issue can be solved by the following, since three are non-working days/holidays we can filter them out via POWERQUERY, than add an Index Column and Another column Which is basically Index column + Number of days wanted, then simply merge duplicate of dates query on Index+number of days wanted column on Index column. And we get the correct date