If statement Calculation that includes nulls and checks for prefix - if-statement

This is for tableau calculated field, but the code is common for any excel or literally anything. Please help its just a simple if statement but with multiple conditions.
I am trying to count a distinct number of customers with a few conditions
customers the with 'ABC' prefix and repeat order = no should be included in this metric
Two customer numbers should be disregarded from the total count
all other customers (regardless of prefix or repeat order selection)
Delivered date should not be null
Below is my calculation
COUNTD(
IF NOT ISNULL([Delivered Date])
AND (
(STARTSWITH([Customer ID],"ABC") AND [Repeat order]="No")
OR NOT STARTSWITH([Customer ID],"ABC") )
AND NOT ([Customer ID] IN ("ABC-AA2517","ABC-CO1325"))
THEN
[Customer ID]
END
)
Numbers
-Overall, I have 11282 customers with a date.
---2239 customers have ABC prefixes.
---Among which 1577 is yes for repeat order and 662 is no for the repeat order.
So the total number for this metric should be 11282-1577-the 2 disregarded customer numbers.
The final number should be 9703, but it shows 8599 as the total.
I think its not include those 660 customers. Not sure what mistake I am making in the calculation.

Related

Getting correct Sum for total row DAX Power BI

I have:
Dim Table:
Accounts: customer granularity level.
Fact Table:
PhoneCalls. calls to customers granularity level.
I need to create and see the number of calls made to a customer up until the customer made their first deposit.
I was able to do this on the customer level but on the Total level I get a weird result:
my measure:
ACC_Calls_2_FDP =
CALCULATE(
COUNTROWS(PhoneCalls),
PhoneCalls[disposition] = "ANSWERED", -- only calls that were answered
PhoneCalls[calldate] <= MAX(Accounts[FDP_Date]), -- up until FDP date per customer
USERELATIONSHIP(Accounts[AccountNo],PhoneCalls[AccountNo]) -- make the connection active
)
the results are:
On the total level i was expecting to see 14. not 536
what is going on? what am i missing?
The data model:
Filter direction: Accounts filters PhoneCalls
Cardinality 1:*
Power BI doesn't sum up each of the row values as in Excel. Instead, for Grand total value they will still calculate it as a row itself with no filters on AccountNo on it.
In this case, the max value of FDP_Date in the whole table is taken in the measure. So for example, ACC 1 has max date of 3, ACC 3 has the max of 10. The grand total calculation will use 10 as the max value and start countrows on whichever rows with FDP_Date < 10.
To fix this is tricky as I don't have any other information than this. However, I hope this explains you well of the "bug".

Calculate daily average including zero in Power BI

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

Power BI count rows with same ID

I have two tables, one with unique ID's ('Risk Finder') and one with many ID's ('All Accidents full'). In 'All Accidents full' there is a column with binary 1s and 0s to denote if something happen within a time range specified ('All Accidents full'[Three Year BAU]). In 'Risk Finder' I'm trying to create a column that counts the number of rows with a 1 in that binary column for each unique ID.
I've tried a combination of Calculate(Countax), Calculate(Sum) and Countrows, but nothing seems to work.
CALCULATE(COUNTAX('All Accidents full','All Accidents full'[Three Year BAU]),'Risk Finder'[ID])
Any help would be greatly appreciated
I'd think you just need COUNT and a filter.
CALCULATE (
COUNT ( 'All Accidents full'[ID] ),
'All Accidents full'[Three Year BAU] = 1
)

Calculating the percentage of grand total by category with DAX for one category only

I need help calculating the percentage of each category in a column (based on the grand total) in DAX but for one specific category.
This is how the data is structured. Each row is an individual transaction with an ID column and item column.
I need to get the % of transactions that are for bagels only. This is my sql code I currently use.
`Select 100 - CAST(count([Items])
- count(case [Items] when 'Bagel' then 1 else null end) AS FLOAT)
/ count([Items]) * 100 as Percent_Bagel
from Items_Table where Items != 'Coffee'
and Items != 'Muffin'`
I need this to be converted to a DAX formula to use in a Power BI measure if possible, but I am new to DAX and don't know where to begin.
Thank you.
The "right" implementation for you always depends on the context. You can achieve your goal through different approaches.
I have used the following:
Measure =
DIVIDE(
-- Numerator: Filter all Bagel's transaction and count them
CALCULATE(
COUNT('Table'[Transaction ID]),
FILTER('Table', 'Table'[Item] = "Bagel")
),
-- Denominator: Remove any filter - essentially fixing the full table - and count all transactions we have
CALCULATE(
COUNT('Table'[Transaction ID]),
ALL('Table'[Item])
),
-- If something goes wrong with the DIVIDE, go for 0
0
)
You may also use the filters to rule out all measures that are not blank.
Without measure filter
With measure filter (Other categories are gone)
Hope this is what you are looking for!

Number of Employees at a particular Branch overtime

I am trying to count the number of employees at a certain branch over different periods of time. I am providing a sample below.
I have an employee table:
A date table:
I have used the formula below to count the number of employees at the "Doe" branch at certain periods of time. I have created inactive relationships between the two tables. I am using the "Year" and "QTR" columns from the date column as filters.
Number of Employees =
CALCULATE(COUNTROWS(tEmployee),
FILTER(VALUES(tEmployee[Enter Date]),tEmployee[Enter Date] <= MAX(tDates[Dates])),
FILTER(VALUES(tEmployee[Leave Date]), OR(tEmployee[Leave Date] >= MIN(tDates[Dates]),ISBLANK(tEmployee[Leave Date]))))
However, my numbers seems to add up/mess up over the two years which is something I am trying to avoid. Like here for example, my number of employees shows 4 for the year 2019 Q1:
Whereas, I have only had one employee in the branch during that time.
I am trying to make this as dynamic as possible. However, I guess I am having trouble trying to figure out what I am doing wrong here.
Any help would be appreciated.