Power BI IF, AND Function - powerbi

I'm attempting to write a DAX Formula that tells me the number of days that a case is opened and closed, and from there to then tell me which case was closed above the average closure day (187) and what was closed below average.
However I need an additional ask within the IF formula to included Cases with Status as Completed.
I tried the following DAX, but this appears to ignoring the Status Completed
Avg to Closure = IF(And(Sheet1[# Days To Close]>=187, Sheet1[Status]"Completed","Above Average","Below Average")
Here is an example of my table.
Table Example
I need a formula to ignore the 2nd account as this currently has a status as open. Is this possible?

First, you might want a measure that calculates average days to close.
daytoclose_avg =
CALCULATE(
AVERAGE(Sheet1[# Days To Close])
)
No, you can build some intelligence for the Avg to Closure. Meaning that it compares the row's days to close to the whole average and returns "Below" or "Above" based on this logic.
Avg To Closure =
VAR avg_allcompleted =
CALCULATE(
[daystoclose_avg],
ALL(Sheet1),
Sheet1[Status] = "Completed"
)
VAR avg_row = [daystoclose_avg]
RETURN
IF(
avg_row > avg_allcompleted,
"Above Average",
"Below Average"
)

Related

DAX Percentage of Count with Conditions

I have a table in power bi in which I have the hours of entry to work. The case is that I can make a table that collects for each hour of work the number of people who have entered at that time with the count function.
See an example:
entry time
09:30
09:30
09:22
In other words, in the table values field I drag entry time and entry time count, which gives me for each entry time the number of people that entered.
The case here is that I would like to create a measure that allows me to obtain the percentage of people who have entered at 9:00 and 9:30 with respect to the total.
[(Number of people that have entered at 9:00)+(Number of people that have entered at 9:30)]/ count of people that have entered
I've unsuccessfully tried to create a measure.
So your table just contains the times when people entered on separate rows. You can count the Timestamps directly with the following measure:
% Count =
VAR selected =
CALCULATE(
Count('Table'[Time]),
'Table'[Time] = TIMEVALUE("9:00")
|| 'Table'[Time] = TIMEVALUE("9:30")
)
VAR allpeople =
CALCULATE(
Count('Table'[Time]),
ALL('Table')
)
RETURN
DIVIDE(selected, allpeople)

How i can compare last week average to todays data in Power BI visual?

I want to show an hourly score for today that refreshes every hour. I built it and it works, but now I want it to compare the hourly average for the last week and if it was higher it shows a red arrow up if lower it shows a green arrow down. I don't have a problem adding the arrows as it's very easy, but I've previously added a column to the query that shows if day = today and then used it as a filter inside the visual to show today's data, so when I try to compare the results the filter also affects the calculation I created:
Measure = calculate(average(rawdata[contacts]),rawdata[Week to Average = 1)
week to average is the column that tells if the week was the previous week is simply if(weekcolumn=weeknum(today())-1,1,0)
Do you know any way i can compare last week average to todays data?
Also visual that i used is a matrix
This is just an idea. So if it is enough to solve the case then ok. If it's not it, then, please, add more info about your data table - a screenshot or sample data. how you calculate your average and what do you mean by average? At least what kind of data you are dealing with is it a sum of some values per day, do you have a different number of values for each day? etc.
measure 1:
averContacts = AVERAGE(rawdata[contacts]) -- no CALCULATE()
measure 2:
avrToday =
CALCULATE(
[averContacts]
,TreatAS({TODAY()}, yourTable[DatesColumn])
)
measure 3:
aveLastWeek =
VAR prevWeekEnd = TODAY() - WEEKDAY(Today(),2) -- 2 -> Mon-Sun week format
VAR prevWeekStart = prevWeekEnd - 7
VAR DatesLastWeek = CALENDAR(prevWeekStart ,prevWeekEnd)
RETURN
CALCULATE(
[averContacts]
,TreatAS(DatesLastWeek, yourTable[DatesColumn])
)
measure for your visual
lastWeek_vs_Today = DIVIDE(avrToday ,aveLastWeek)

Average Days Outstanding of ALL tickets as of certain date

I am trying to figure out Dax for Average Days Outstanding taking into account all tickets that are open as of spec date to show trend over time.
How many days tickets are outstanding on average over the period of time and how many days outstanding on tickets as of 8/25/2020? In this case, I am taking as of today's date but I need to be able to calculate as of 8/1/2020 as well.
Per example below, I have manually calculated in excel the outcome that I am looking for but do not know how to create DAX formula to reflect that. I did it in excel to show what outcome I am trying to achieve.
In powerbi, I do have Calendar table with a date column and with relationship to ticket date.
As you can see, in my average formula I am taking into consideration all tickets that have current balance >0 as of 8/25/2020 and giving average counting prior rows ( prior tickets open as of 8/25/20).
Currently, I know to how to calculate DAX days outstanding which is difference between ticket dates and today on tickets with current balance >0 but I do not know how to include in my DAX prior days where tickets are outstanding as well.
Your help is greatly appreciated!
Try with the below logi-
running_average =
VAR current_row_date = MIN(your_table_name[payment date])
RETURN
AVERAGEX (
FILTER(
ALL(your_table_name),
your_table_name[payment date] <= current_row_date
),
your_table_name[outstanding amount column name]
)
I can see some value in "payment date" column is blank. They will produce wrong results here. You need to somehow make adjustment for those BLANK values to get correct results.

How can I calculate a monthly average including months with no records in Power BI

I'm trying to calculate a monthly average number of cases for each investigator. It might be over a quarter, year, or multiple years so it needs to respond to the visual or table context I drop it into. The base table has Case (individual case#), Investigator (person name), Date assigned (not shown), and from that date,month and year columns extracted and a YearMonth categorical column.
I create a caseCount measure as
caseCount = COUNT('Table'[Case])
I've tried several different ways to calculate the average over all months (in this case 4). Because Mary has cases in each month, her average is correct (1.75) but Sam's uses a denominator = 3, thus doesn't calculate correctly. (returns 1.3 instead of 1). How can I force the calculation to use the full number of months.
Additional notes:
There may be cases in the table that fall outside the date range I want so I've tried using a
Avg = CALCULATE(AVERAGE(caseCount), Table[Date] > #10/31/2019#)
I've also tried several variations using CALCULATE(DIVIDE(), [Date] > 10/31/2019. Everything seems to exclude those months when an investigator had no investigations assigned. I also tried connecting to a Date table and using the Distinct YearMonth value created there.
This is because the evaluation context.
I would define the measure as follow:
VAR _casecount = //count number of cases in the selected period, applied on the fact table
VAR _months = COUNTROWS(CALCULATETABLE(VALUES('Calendar'[Month]), ALLSELECTED('Calendar'))) //count number of months in the selected period
RETURN
_casecount/months
Update
I did not consider the scenario of multi-year periods involving May-2019 and May-2020. Then, let's reframe the solution using DATEDIFF:
VAR _casecount = //count number of cases in the selected period, applied on the fact table
VAR _firstCalendarDate = CALCULATE(MIN('Calendar'[Date], ALLSELECTED('Calendar'))
VAR _lastCalendarDate = CALCULATE(MAX('Calendar'[Date], ALLSELECTED('Calendar'))
VAR _months = DATEDIFF(_firstCalendarDate, _lastCalendarDate, MONTH)
RETURN
_casecount/months

Power BI Rolling Average

I've used the new Quick Measures feature of Power BI to build a 3 month rolling average calculation and it's working well. The equation is displayed below. However, when I try to use this metric in a time series visualization, the calculations are displaying three months past the current month, but I'd like for the calculation to stop at the current month.
I've played around with the __DATE_PERIOD variable to no avail. My date filter for the page is set to show all dates in the current months or 12 months prior via a calculated column on the date table.
Is anyone aware of how I can get the visualization to end at the current month?
Average Days to Close Rolling Average =
IF(
ISFILTERED('Date'[Date]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy."),
VAR __LAST_DATE =
ENDOFMONTH('Date'[Date].[Date])
VAR __DATE_PERIOD =
DATESBETWEEN(
'Date'[Date].[Date],
STARTOFMONTH(DATEADD(__LAST_DATE, -3, MONTH)),
__LAST_DATE
)
RETURN
AVERAGEX(
CALCULATETABLE(
SUMMARIZE(
VALUES('Date'),
'Date'[Date].[Year],
'Date'[Date].[QuarterNo],
'Date'[Date].[Quarter],
'Date'[Date].[MonthNo],
'Date'[Date].[Month]
),
__DATE_PERIOD
),
CALCULATE(
'Closed Opportunities'[Average Days to Close],
ALL('Date'[Date].[Day])
)
)
)
In order to limit what is displayed within your chart, you need to filter the applicable date field so it only displays the dates you desire. In this case, you only want it to include dates <= today.
In order to automatically filter it when it is refreshed, I typically add a custom DAX column to the date table that I can filer on. In this case it would be something along the lines of:
excludeFutureDatesInd = 'Date'[Date] <= TODAY()
You can then add a visual, page, or report filter selecting all dates where [excludeFutureDatesInd] = True.
Not sure if you're still having issues with this, but I'd like to share a hack fix for those landing here. I fixed this issue by filtering on the base data (in your example, this would be "Average Days to Close"). Set a visual-level filter to include only those items where Average Days to Close > 0, and you should get the extra dates cut off the end of the graph.
So long as all of your base data passes through the filter, you should be good.