Sum of Value by Category and Status Power BI - powerbi

I am Attempting to get a sum of ppl that is based on the status of the offer
here is an example of the data
Acc
PPL
Offer comp
ABCDE
1
1
ABCDE
1
0
ABCDE
1
0
CDEFG
2
0
DEFGH
1
1
DEFGH
1
0
DEFGH
1
0
DEFGH
1
1
EFGHI
2
1
EFGHI
2
1
EFGHI
2
0
And this is what I am wanting to achieve as a column either in Power query or Dax
Account
PPL
Offer Comp
total
ABCDE
1
1
1
ABCDE
1
0
1
ABCDE
1
0
1
ABCDE
1
0
1
BCDEF
1
0
1
BCDEF
1
0
1
CDEFG
2
0
2
DEFGH
1
1
2
DEFGH
1
0
2
DEFGH
1
0
2
DEFGH
1
1
2
EFGHI
2
1
4
EFGHI
2
1
4
EFGHI
2
0
4
Basically, we are looking for a count of ppl where if the offer is not completed, we count the ppl, if the offer is accepted, we count the ppl for each acceptance.
Hope this makes sense I would then use this column to create measures as they want ppl count to be unique unless they accepted multiple offers on same account.
I tried to create with selected values and all except but I just couldn't get it to work.
I am probably completely overthinking this.

Very cryptic, and I must admit the sample data and your description is not great, but I think you are after something like this:
Total =
VAR _offer =
SUMX (
ALLEXCEPT (
'Table' ,
'Table'[Account]
) ,
'Table'[Offer Comp] * 'Table'[PPL]
)
RETURN
IF (
_offer > 0 ,
_offer ,
CALCULATE (
MAX ( 'Table'[PPL] ) ,
ALLEXCEPT (
'Table' ,
'Table'[Account]
)
)
)

I have figured it out with help from Marcus
thank you. I realised also need to add a date grouping.
here is the code
Total =
VAR _offer =
CALCULATE(SUM(Table[Offer Comp]),
ALLEXCEPT(Table, Table [Account], Table[Date]))
VAR Pax =
CALCULATE(MAX(Table[PPL]),
ALLEXCEPT(Table, Table[Account]))
RETURN
IF(_offer = 0,
Pax *1,
pax*_offer
)

Related

create a countif measure not impacted by filter in visual in powerBI

Need help in creating measures that will reflect the actual count of rows in the table when filtered.
Example:
ID
RankC
RankA
Avg Diff
RankC_count
RankA_count
Avg Diff_count
1000
AAA
XYZ
+01.00 to +01.25
5
6
4
1001
AAA
ZY1
+01.5.00 to +01.75
5
1
5
1002
AAB
XYZ
+01.5.00 to +01.75
3
6
5
1003
AAB
ZY2
+01.5.00 to +01.75
3
1
5
1004
AAB
XYZ
+01.00 to +01.25
3
6
4
1005
AAA
XYZ
+01.00 to +01.25
5
6
4
1006
AAA
ZY3
+01.00 to +01.25
5
1
4
1007
AAC
XYZ
+01.25.00 to +01.5
1
6
2
1008
AAA
ZY4
+01.25.00 to +01.5
5
2
2
1009
AAZ
ZY4
+01.5.00 to +01.75
1
2
5
1010
ABY
XYZ
+01.5.00 to +01.75
1
6
5
The last 3 columns represent the count of each entry. If I use the measure such as below, it provides the correct count. However, when I use in the visual, filtering by ID, say ID 1000, I want it to show line 1 with 5,6, and 4 on the counts, instead of all 1.
Questions:
Is there any measure to give me the correct result? say summarize the table first then do a lookup?
is creating a column the only choice? I cannot create columns since I need 1000 of these calculated columns. whereas using measure, I can create 1000 in one go.
Thanks for any help.
AverageDiff_Count =
CALCULATE (
COUNTROWS (
FILTER ( '28Jun_1973', [Average Diff] = '28Jun_1973'[Average Diff] )
)
)
The ALL function is useful here. It removes filter context so that it uses the whole table instead of just the part in the current filter context.
AvgDiff_Count =
VAR CurrAvgDiff = SELECTEDVALUE ( '28Jun_1973'[Avg Diff] )
RETURN
COUNTROWS (
FILTER ( ALL ( '28Jun_1973' ), '28Jun_1973'[Avg Diff] = CurrAvgDiff )
)

How to count rows in a column, per period, and per category, then rank them, in Power BI using DAX?

Below is an example of what I am trying to do.
I have the first two columns in the Table and want to add column three and four.
Is there a DAX way to do this?
I've tried GROUPBY, SUMMARIZE and RANX but can't seem to work it out.
period
animal
count_animal_per_period
rank_animal_per_period
Period 1
cat
4
1
Period 1
cat
4
1
Period 1
dog
2
2
Period 1
cat
4
1
Period 1
dog
2
2
Period 1
bird
1
3
Period 1
cat
4
1
Period 2
dog
4
1
Period 2
dog
4
1
Period 2
dog
4
1
Period 2
cat
2
2
Period 2
cat
2
2
Period 2
bird
1
3
Period 2
dog
4
1
Period 3
cat
4
1
Period 3
bird
2
2
Period 3
cat
4
1
Period 3
cat
4
1
Period 3
dog
1
3
Period 3
bird
2
2
Period 3
cat
4
1
Two calculation:
Count_animal_per_periods =
var __count = CALCULATE( COUNTROWS('Sheet2 (2)'), FILTER(ALL('Sheet2 (2)'[animal],'Sheet2 (2)'[period]), 'Sheet2 (2)'[animal] = SELECTEDVALUE('Sheet2 (2)'[animal]) && 'Sheet2 (2)'[period] = SELECTEDVALUE('Sheet2 (2)'[period])))
return
__count
And second:
rank_animal = RANKX (
FILTER(
ALL(
'Sheet2 (2)'[period],
'Sheet2 (2)'[animal]
),
'Sheet2 (2)'[period] = MAX('Sheet2 (2)'[period])
),
[Count_animal_per_periods]
)

PowerBI DAX Measure to return 1 for all the items in category if any item in category has result 1

I need measure (or maybe calculated column) which will return 1 in the column called Final when any of the item Table2[name] per category (Table1[Category]) is 1. So even if there is just one item in the category that has result 1 it returns 1 for all of them in the same category and 0 when all the items in the category have result 0. Hope the example below is clear.
Table1[Category]
Table2[name]
Result
Final
A
A:1
0
1
A
A:2
1
1
A
A:3
0
1
B
B:1
0
0
B
B:2
0
0
C
C:1
1
1
C
C:2
0
1
Using variables you can access the Category to filter on the table and obtain the MAX result.
Calculation: Calculated Column
Final =
VAR CurrentCat = [Table1[Category]]]
VAR MaxResult =
MAXX ( FILTER ( 'Table', [Table1[Category]]] = CurrentCat ), [Result] )
RETURN
IF ( MaxResult = 1, 1, 0 )
Output
Table1[Category]
Table2[name]
Result
Final
A
A:1
0
1
A
A:2
1
1
A
A:3
0
1
B
B:1
0
0
B
B:2
0
0
C
C:1
1
1
C
C:2
0
1

Amazon Athena: Query to find out patients with compliance=0 for consecutive 10 days

Find all patients having compliance=0 from past consecutive 10 days from current date using Amazon Athena.
patient id compliance create_date
1 0 2021-01-01
1 0 2021-01-02
1 0 2021-01-03
1 0 2021-01-04--rejected not for consecutive 10
2 0 2021-01-01
2 0 2021-01-02
2 0 2021-01-03
2 0 2021-01-04
2 0 2021-01-05
2 0 2021-01-06
2 0 2021-01-07
2 0 2021-01-08
2 0 2021-01-09
2 0 2021-01-10-- accepted as for 10 consective
There are multiple ways to achieve this, and one can be to take the difference between a given date and the next one and check the cumulative sum of last X deltas (which is equal to 10 in your case) and the cumulative sum of your compliance integer on that row (which should be strictly equal to 0):
with base as (
select
*,
sum(delta) over (partition by patient_id rows between 10 preceding and current row) as cumdelta ,
sum(compliance) over (partition by patient_id rows between 10 preceding and current row) as cumcompliance
from (
select *, if (date_diff('day', date, next_date) is null, 1, date_diff('day', date, next_date)) as delta
from (
select
patient_id,
compliance,
try_cast(date as date) as date,
lead(date) over (partition by patient_id order by date) as next_date
from data
)
)
)
select
patient_id,
compliance,
date,
case when (cumdelta = 10 and cumcompliance = 0) then 'yes' else null end as validated_compliance
from base

Power BI : DAX: Running Sum with fixed start date - even when filtering

I have two tables, with:
Entrydate, several categories
ChurnDate, several categories
The categories are connected via different tables, and the dates are connected with a Calendar.
Now I want to calculate how many customers I have. So I have following DAX formulas
1. SumChurn =
CALCULATE(
SUM('kuendigungen'[KUENDIGUNG]);
FILTER(
ALLSELECTED('Calendar'[Date]);
ISONORAFTER('Calendar'[Date]; MAX('Calendar'[Date]); DESC)
)
)
2. SumEntry =
CALCULATE(
SUM('eintritt'[NEUMITGLIED]);
FILTER(
ALLSELECTED('Calendar'[Date]);
ISONORAFTER('Calendar'[Date]; MAX('Calendar'[Date]); DESC)
)
)
3. TotalCustomers = SumEntry - SumChurn
This works, but in my diagram I want to filter the dates, so that it only visualizes 2020 or the last 3 years.When I do this the calculation is wrong because it only counts in this interval.
Is there a solution that I can filter the date in my visuals but in my calculation the start date of the cummulative sum is always fixed?
I dont't want a new column because I still want to filter my categories of customers...
Thanks,
Michaela
Edit: Try to explain it clearer
Example Table 1: contains new customers
Date unique_id1 unique_id2 unique_id3 cat1 cat2 cat3 cat4 cat5 cat6
1886-02-01 2070030124 550261 207000152145 207 0 0 1 0 0
1887-01-01 4350002756 4081878 435000010707 435 0 0 1 0 0
1888-01-01 7030000597 3206858 703000001279 703 0 0 1 0 0
1888-06-01 7030016696 3208056 703000005002 703 0 0 1 0 0
1888-09-01 8210024182 204124 821000008664 821 1 0 1 0 1
1889-01-01 7050055324 1988250 705000018309 705 1 0 1 0 0
1889-01-01 8250000278 439485 825000600296 825 0 0 1 0 0
1889-05-01 7030023754 3208355 703000000884 703 0 0 1 0 0
1889-10-01 2110071206 2849359 211000330019 211 0 1 1 0 0
1889-10-01 2110071236 2851371 211000120014 211 0 0 1 0 0
1889-11-14 5190529889 4260192 519000123846 519 1 0 1 0 0
1890-07-01 7330349030 4819467 733000013102 733 0 0 1 0 0
1890-07-01 7330152914 4817492 733000075604 733 1 0 1 0 1
1890-07-01 8190000889 486170 819000215708 819 0 0 1 0 0
1890-07-01 8190444976 486199 819000215740 819 0 0 1 0 0
1890-12-01 8190001388 476049 819000100005 819 0 0 1 0 0
1891-01-01 7030001248 3206975 703000000043 703 0 0 1 0 1
Example Table 2: contains leaving customers
similiar to table 1
Example Calendar Table:
01.01.1990
02.01.1990
03.01.1990 ... (till today)
Output shut be a measure
for each day in calendar: number of customer at this date = cumulative_sum(newcustomer) - cumulative_sum(churncustomer)
I get exactly this output, when I run the calculations I wrote, but I want the measure in a way, ehen I filter the date, the sum is still the cummulative sum from the very first date, otherwise the numbers are wrong.
Edit3:
I did exactly the same thing, as mkrabbani posted, but it doesnt't work for me, following calculations:
TotalKuendigungen =
CALCULATE(
SUM('kuendigungen'[KUENDIGUNG]);
FILTER (
ALL ( 'Calendar'[Date] );
( 'Calendar'[Date] <= MAX ( ( 'Calendar'[Date] ))
)))
TotalNeukunden = CALCULATE(
SUM('eintritt'[NEUMITGLIED]);
FILTER (
ALL ( 'Calendar'[Date] );
( 'Calendar'[Date] <= MAX ( ( 'Calendar'[Date] ))
)))
AnzahlMitglieder = [SummeNeumitglied] - [SummeKuendigung]
This is how it looks for me: (Neukunden: new customers, kündigungen: leaving, aktuellemitglieder: number of customers)
Picture 1 correct calculation
Picture 2: also correct calculation, but filter doesnt work
thanks for adding some sample data with more explanation. If I get your requirement correct, this below steps with explanation will help you solving your issue I hope.
Assumption: If my understanding is correct, you have 3 tables with Date, new_customer and leaving_customer and they are related as below diagram shown.
Now, I have created some sample data for 10 days, to visualize your requirement/issue. Hope, cumulative counts in the below table is correctly calculated (using basics of cumulative calculation).
At this stage, you need a measure that will calculate current number of customer for each row based on calculation > "cumulative_new_customer - cumulative_leaving_customer" which is not a tough job for you.
But, you are having issue when you are slicing your data using Date slicer. If you are selecting date number 5, which is "January 05 2020" in my sample data. You wants the final counts based on date January 01 to 05, but you are getting only counts from one single date "January 05 2020".
If the above explanation is correct, I would suggest to write 3 separate Measure as explained below in this answer. You can have a look on the output in the below picture I have added with comparison with before and after slicing the data. You can see the number of current user for "January 05 2020" is 41 for both case (Before and After Slicing)
Now, if everything above is meeting your expectation, you can use this below 3 measures as written.
1.
cumulative_new_customer =
CALCULATE (
COUNT(new_customer[unique_id]),
FILTER (
ALL ( 'Dates'[Date] ),
'Dates'[Date] <= MAX ( 'Dates'[Date] )
)
)
2.
cumulative_leaving_customer =
CALCULATE (
COUNT(leaving_customer[unique_id]),
FILTER (
ALL ( 'Dates'[Date] ),
'Dates'[Date] <= MAX ( 'Dates'[Date] )
)
)
3.
number_of_cutomer_today = [cumulative_new_customer] - [cumulative_leaving_customer]
Hope the above details will help you.