create a countif measure not impacted by filter in visual in powerBI - 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 )
)

Related

SUM distinct values in DAX, Power BI

I am quite new to Power BI and now facing a problem, illustrated below.
Here
**DATA**
LEVEL | PROJECT NAME| BUDGET_TYPE | BUDGET_CODE | BUDGET_AMOUNT
1 xxxx A 0000001 4,800,000
1 xxxx A 0000002 4,300,000
1 xxxx A 0000002 900,000
1 A 0000003 1,300,000
1 A 0000004 4,780,000
1 A 0000010 3,900,000
1 A 0000010 3,900,000
1 A 0000011 200,000
1 A 0000015 1,028,165
1 A 0000015 1,028,165
1 B 0000016 83,000,000
1 B 0000017 83,000,000
1 B 0000017 28,200,000
1 B 0000018 15,000,000
1 B 0000019 4,800,000
1 B 0000020 7,000,000
1 B 0000020 7,000,000
PIVOT TABLE from Excel
Row Labels | Max of BUDGET_AMOUNT
LEVEL: 1 83,000,000
TYPE:A 4,800,000
0000001 4,800,000
0000002 4,300,000
0000003 1,300,000
0000004 4,780,000
0000010 3,900,000
0000011 200,000
0000015 1,028,165 **20,308,165**
TYPE:B 83,000,000
0000016 83,000,000
0000017 83,000,000
0000018 15,000,000
0000019 4,800,000
0000020 7,000,000 **192,800,000**
Grand Total 83,000,000
I have raw data and PIVOT TABLE, respectively. What I want is to find MAX amount of each CODE first.
Then, SUM those MAX values only with distinct CODE.
I have attached the PIVOT TABLE for simplifying my problem. What I really want at the end is the SUM amount which is
LEVEL 1
TYPE A: 20,308,165
TYPE B: 192,800,000
Is there any way I can do that? Please help. Thanks!
You can do a measure that summarizes the different codes, calculates the max for each code, and sums up this table row-by-row:
Sum :=
VAR _tbl =
SUMMARIZE (
'Table' ,
'Table'[Level] ,
'Table'[Budget Code] ,
"Max Budget" , CALCULATE ( MAX ( 'Table'[Budget] ) )
)
RETURN
SUMX ( _tbl , [Max Budget] )
See minimal data example here:

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

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

Calculate rolling average and use for missing values

I am trying to build a model in PowerBI that forecasts future values based certain inputs. The issue I am running into is that some of my data is missing rows, and I also need to use the last value of data as the one for the next period, to which I will add additional (calculated) values. I used an index that starts at -5 and goes through 30, and added a date column expressed as Today()+Index. The data looks like this:
Index | Date Index | Values |
------------------------------
-6 11/4 2
-5 11/5 5
-4 11/6 7
-3 11/7
-2 11/8 5
-1 11/9 4
0 11/10 <-- This is today
1 11/11
2 11/12
...
What I need the data to look like is this:
Index | Date Index | Values |
------------------------------
-6 11/4 2
-5 11/5 5
-4 11/6 7
-3 11/7 4.667
-2 11/8 5
-1 11/9 4
0 11/10 4 <-- This is today
1 11/11 4
2 11/12 4
...
The Dax Formula I have is:
Values =
If(
AND(
SUMX(
Filter(
Table 2,Table 2[Date]='Summary Table'[Date Index]
),
Table 2[Initial Values]
)=0,
'Summary Table'[Date Index]<Today()
),
Calculate(
Averagex(
Table 2,Table 2[Initial Values]
),
DATESINPERIOD(
Table 2[Date],Today()-1,5,DAY
)
),
SUMX(
Filter(
Table 2,Table 2[Date]='Summary Table'[Date Index]
),
Table 2[Initial Values]
)
)
Note that I am pulling in my values from a different table, Table 2, which has multiple dates with values, which I am summing in this summary table by date. For some reason, the formula is filling in an incorrect average value for missing values, and not pulling the last value (from Today()-1) through to the end of the data. Any help or advice would be appreciated!

dax calculation for costume measure

i have a 2 tables that looks like this
Key |Num Of Treatments| Cost |
1 2 1000
1 2 1500
1 2 2000
2 3 700
3 3 800
4 4 900
key | limit |
1 1
2 1
3 2
4 3
the calculation that i want to do on dax is : (Num Of Treatments-Limit)*cost/Num Of Treatments
Assuming that the key column is unique for the second table (Table2 in dax).
Calculation =
VAR _limit =
LOOKUPVALUE ( Table2[limit], Table2[key], [key] )
RETURN
DIVIDE ( ( [Num Of Treatments] - _limit ) * [cost], [Num Of Treatments] )
This can be easily be achieved after creating one to many relationship between two tables with column Key.
Dax formula :
New Measure = ((SUM(Asset[No Of Treatments])-SUM(Tickets[Limit]))*SUM(Asset[Cost]))/SUM(Asset[No Of Treatments])