SUM distinct values in DAX, Power BI - powerbi

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:

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

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

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

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.