How to remove blank rows in a calculated Power BI table? - powerbi

I have a days to report measure where I perform some calculation on each row for the numerator and then filter out blank rows for the denominator. Example table, code and result as follows:
Team | Meeting | Report
aaa | 1/1/2018 | 9/1/2018
aaa | 1/1/2018 | 7/1/2018
bbb | 1/1/2018 | 1/2/2018
bbb | 1/1/2018 |
ccc | 1/1/2018 | 3/3/2018
aaa | 1/1/2018 |
This is my function for the average days
CALCULATE(
AVERAGEX(Planning,Planning[Report]-Planning[Meeting]),
FILTER(Planning,NOT(ISBLANK(Planning[Report])))
)
And I'd like:
Team | average
aaa | 7 (14/2)
bbb | 31 (31/1)
ccc | 61 (61/1)
Function seems to work but I'm slightly paranoid about my (lack of) understanding of CALCULATE and FILTER than I may be doing something wrong?

Your function looks fine. The FILTER removes any rows with a blank Report value and then the AVERAGEX evaluates for just those rows.
FYI, for this construction, you don't necessarily need FILTER you can just write the following since CALCULATE supports basic filtering:
Average = CALCULATE(AVERAGEX(Planning, Planning[Report] - Planning[Meeting]),
NOT(ISBLANK(Planning[Report])))
Another way to do this is to use FILTER inside of AVERAGEX instead of using CALCULATE:
Average = AVERAGEX(FILTER(Planning, NOT(ISBLANK(Planning[Report]))),
Planning[Report] - Planning[Meeting])

Related

How to find YTD daily average in Power Bi?

I am trying to figure out how to calculate a "New Measure" in my power BI visual that calculates the YTD daily average.
So for example,
my query on the backend would look like this
App Date | ID | Subject
01\01\2022 | 123 | Math
01\01\2022 | 456 | Science
01\02\2022 | 789 | Science
01\02\2022 | 012 | History
01\03\2022 | 345 | Science
01\03\2022 | 678 | History
01\03\2022 | 921 | Art
01\03\2022 | 223 | Science
01\04\2022 | 256 | English
Im trying to calculate what the daily average is YTD for math, science, history etc.
I tried
1 Daily average = calculate (average(Query[app date]))
I know this is not correct but I would appreciate any help..
Measure= AVERAGEX(VALUES(Date[APP_DATE]), [Subject])
This forumula should work to count YTD Average
First you need to create a date table using New Table and paste this code:
Date =
VAR ModelDate =
ADDCOLUMNS(
CALENDAR(DATE(2022,01,01), DATE(2022,12,31)),
"Year", YEAR([Date]),
"Month",FORMAT([Date],"mmm"),
"MonthNumber", MONTH([Date]))
RETURN
ModelDate
Then Create a relationship between date table and your fact table:
Then Go on to write this code AS Your measure:
Daily Avg_YTD =
CALCULATE(
AVERAGEX(
VALUES(YourTbl[Subject]),COUNT(YourTbl[Subject])),
DATESYTD('Date'[Date]))
Now If you put it on a table visual:
I hope It solves your problem!

How do I calculated group averages with a filter in DAX?

I've got data across 2 tables that kind of looks like:
Table 1
Mean | Activity | name_id
---------------------------
1 | Swimming | 1
3 | Basketball | 2
3 | Swimming | 3
9 | Running | 1
5 | Basketball | 3
TypeName | Name_id
------------------
ABC | 1
DEF | 2
GHI | 3
Assuming joins are in place, I want to run the equivalent of this SQL:
select activity, avg(mean)
from table1 a
inner join table2
on table1.name_id = table2.name_id
where table2.name_id = 'DEF'
I got a basic quick measure that does everything except for the filter for 'DEF' name_ids:
Average =
AVERAGEX(
KEEPFILTERS(VALUES('Table1'[Activity])),
CALCULATE(SUM('Table1'[Mean]))
)
I'm stumped as to where exactly I should put a filter. I also tried using CALCULATE as the base function, but was stumped on how to properly run that.

Power BI/DAX Query - Finding value against range on another table

So I have one table that has unique invoices as its rows, with a vendor name and an invoice cost among its columns, something like:
Invoice | Vendor | Cost
AAA | Good Co| $10
BBB | Good Co| $15
CCC | Best Co| $30
DDD | Bad Co | $50
And created a custom column to give me a total spend for each vendor:
VendorGrandTotal =
CALCULATE(SUM('Raw Data'[Cost]),ALLEXCEPT('Raw Data','Raw Data'[Vendor]))
To get a result like:
Invoice | Vendor | Cost | Total
AAA | Good Co| $10 | $25
BBB | Good Co| $15 | $25
CCC | Best Co| $30 | $30
DDD | Bad Co | $50 | $50
Meanwhile I have another table that depicts percentage of rebate from the vendor based on if the total spend with them is above or below a certain amount. Something like:
Vendor | Tier 1 % | Tier 1 From | Tier 1 To | Tier 2% | Tier 2 From
Good Co | 1% | $0 | $20 | 2% | $20
...and so on.
So in the case of this example we should be getting a rebate of 2% since the total spend of the invoices with Good Co adds up to greater than $20. But I'm stumped for how to execute this automatically within Power BI. Is there a way to produce a column or table somewhere that checks that the vendor in the invoices table is the same one from the rebate tier list AND can check against the different tier levels to see what percentage the rebate is AND return that percentage as a result that other calculations can be run against?
I'd suggest unpivoting your rebate table to look like this instead:
Vendor | Tier | From | To | Rebate
Good Co | 1 | $0 | $20 | 1%
Good Co | 2 | $20 | | 2%
Then you can look up the appropriate rebate percent by taking the maximal matching row where VendorGrandTotal is greater than From.
As a calculated column on 'Raw Data', you can do this:
Rebate % =
MAXX(
FILTER(Rebates,
Rebates[Vendor] = EARLIER([Vendor]) &&
Rebates[From] < EARLIER([VendorGrandTotal])
),
Rebates[Rebate]
)

Calculating average NETWORK days Power BI

I've been reading various threads and guides to performing a 'NETWORKDAYS' style calculation in Power BI but struggling to make it work.
I have a table like this:
Team | Meeting | Report
aaa | 1/1/2018 | 9/1/2018
aaa | 1/1/2018 | 7/1/2018
bbb | 1/1/2018 | 1/2/2018
bbb | 1/1/2018 |
ccc | 1/1/2018 | 3/3/2018
aaa | 1/1/2018 |
And I want to return the average days without weekends and holidays, something like this:
Team | average
aaa | 5 (10/2)
bbb | 23 (45/1)
ccc | 45 (45/1)
I have this function, which seems to work albeit clunkily, but I don't know how to remove the non-weekdays from the Date table:
AVERAGEX(FILTER(Planning,NOT(ISBLANK(Planning Actual_FinalReport]))),
(COUNTROWS(DATESBETWEEN(DateTable[Date],
Planning[Actual_ClosingMeeting],Planning [Actual_FinalReport]))
))
Where DateTable is:
Date | Weekday
5/1/2018| 1
6/1/2018| 0
7/1/2018| 0
and so on...
Essentially, I want to iterate over Planning (filtering out blanks in [Report]) and count the dates between Meeting and Report from the Dates table, filtered by Weekday = 1. It's the syntax to link the tables I think I'm struggling with.
Modifying your formula a bit, how about something like this?
Average = AVERAGEX(
FILTER(Planning,
NOT(ISBLANK(Planning[Actual_FinalReport]))),
COUNTROWS(
FILTER(DateTable,
DateTable[Date] IN DATESBETWEEN(
DateTable[Date],
Planning[Actual_ClosingMeeting],
Planning[Actual_FinalReport]) &&
DateTable[Weekday] = 1)))
I'm adding the Weekday filter at the end.

Total/Sum working incorrectly in Power Bi

I have created a Report in which I have created some measures like -
X =
CALCULATE (
DISTINCTCOUNT ( ActivityNew[Name] ),
FILTER (
ActivityNew,
ActivityNew[Resource Owner Name] = MAX ( 'Resource Owners'[Manager Name] )
&& ActivityNew[LocationId] = 2
)
)
When I use this measure in table then the column values dont add up. For eg. if the value of this measure is 2,2,2,2,2 then Total in table should be 10. but it is showing 2.
I have noticed that wherever I have used this MAX(), the measure values are not adding up.
Why this is happening and Is their any solution for this?
You are using DISTINCTCOUNT which is in general not aggregatable.
Say you have the following table Sales:
+----------+------+-------+
| Customer | Item | Count |
+----------+------+-------+
| Albert | Coke | 3 |
| Bertram | Beer | 5 |
| Bertram | Coke | 2 |
| Charlie | Beer | 1 |
+----------+------+-------+
If you wanted to count the number of distinct items each customer has bought, you would create a new measure with the formula:
[Distinct Items] := DISTINCTCOUNT(Sales[Item])
If you include the [Customer] column and your [Distinct Items] measure in a report, it would output the following:
+----------+----------------+
| Customer | Distinct Items |
+----------+----------------+
| Albert | 1 |
| Bertram | 2 |
| Charlie | 1 |
+----------+----------------+
| Total | 2 |
+----------+----------------+
As you can see, this does not sum up, as the context of the total row, is the entire table, not filtered by any particular customer. To change this behaviour, you have to explicitly tell your measure that it should sum the values derived at the customer level. To do this, use the SUMX function. In my example, the measure formula should be changed like this:
[Distinct Items] := SUMX(VALUES(Sales[Customer]), DISTINCTCOUNT(Sales[Item]))
As I only want to sum over unique customers I use VALUES(Sales[Customer]). If you want to sum over every row in the table simply do: SUMX(<table name>, <expression>).
With this change, the output in the above example would be:
+----------+----------------+
| Customer | Distinct Items |
+----------+----------------+
| Albert | 1 |
| Bertram | 2 |
| Charlie | 1 |
+----------+----------------+
| Total | 4 |
+----------+----------------+