dax calculation for costume measure - powerbi

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

Related

create a measure count of values and blank in a column with dax

I have these columns and I want to create a measure to calculate the number of types
types
ID Type
1 A
2 A
3 B
4 C
5 D
6 A
7
8
Results :
A 3
B 1
C 1
D 1
Blank 2
Try this Calculated Table:
Results =
SUMMARIZE(
'Table',
'Table'[Type],
"Count", COUNT('Table'[ID])
)

SUM of DISTINCTCOUNT

I need to sum of different suppliers by produt_id, below is an example of table:
product_id
supplier
1
A
1
B
2
A
2
C
2
C
3
D
4
A
4
B
4
B
4
E
4
E
5
C
5
F
5
F
In the table I have other elements, but these columns are the relevant ones for the count.
The answer I'm looking for is 10, I try to explain in the table below:
product_id
DistinctCount(supplier)
1
2
2
2
3
1
4
3
5
2
Total = 2 + 2 + 1 + 3 + 2 = 10
Thanks in advance!
I tried several different ways and I couldn't. I hope I can solve my problem.
These 2 measures will solve your problem
Count Supplier = DISTINCTCOUNT('Table'[supplier])
and
Sum Count Supplyer =
SUMX(
DISTINCT( 'Table'[product_id] ),
[Count Supplier]
)
The Total of Count Supplier is the number of distinct suppliers in the whole table, ignoring the grouping.

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

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!