PowerBI: Total Count Distinct - powerbi

I need some help with my measure counting correct in total.
This is the MainTable:
With my Measure "CountPass" I count every ID distinct with the action "pass":
CountPass = CALCULATE(DISTINCTCOUNT(Workflow[ID]),Workflow[action]="pass")
Furthermore with my Measure CountPassPreweek I do same with reference on previous Week by using the a date table:
CountPassPreweek =
var currentweek = SELECTEDVALUE(DateTable[WeekNum])
var currentweekday = SELECTEDVALUE(DateTable[WeekNo])
var currentyear = SELECTEDVALUE(DateTable[Year])
var maxweeknum = CALCULATE(MAX(DateTable[WeekNum]),all(DateTable))
Return
SUMX(
if(currentweek = 1,
DateTable[WeekNo] = currentweekday && DateTable[WeekNum] = maxweeknum && DateTable[Year] = currentyear - 1,
DateTable[WeekNo] = currentweekday && DateTable[WeekNum] = currentweek -1 && DateTable[Year] = currentyear)),
[CountPass]
)
This is working so far but not showing the totals, so I have a second measure for doing that:
CountPreweekTotal =
var _table = SUMMARIZE(DateTable,DateTable[Date],"_value",[CountPassPreweek])
return
SUMX(_table,[_value])
And here you see my problem: The measure doesn't count distinct like the "original" counting Measure as you see here
Hope somebody can help me with that.
Thanks a lot!

It's counting 3 since abc is getting counted twice the way your measure is written (since dates are separated in your SUMMARIZE).
Since you appear to have a proper date table, you should be able to use time intelligence functions to write this much more simply as
CountPassPreviousWeek =
CALCULATE ( [CountPass], DATEADD ( DateTable[Date], -7, DAY ) )
This should work for the total too.

Related

Calculating single values from cumulative data in Power BI

In my sales table I would like to change cumulative values into single values. Here is sample data from my table.
I created a measure that as far as I know should works for this.
sales_single_values = VAR current_sales = SUM('sales'[sales cumulative]) VAR prev_sales SUM('sales'[sales cumulative]) - CALCULATE( current_sales, 'sales'[period] = 'sales'[period] - 1) Return IF(ISBLANK(prev_sales), BLANK(), current_sales - prev_sales)
But unfortunately the final result on the chart is still the same as I used cumulative values, not single ones. Any ideas what should I change in my measure?
Expected values would be:
Period 1: 4
Period 2: 2
Period 3: 7
As a measure, something like:
sales_single_values =
var prev = max('sales'[period]) - 1
var prev_sales = CALCULATE( SUM('sales'[sales cumulative]), 'sales'[period] = prev)
Return
sum(sales[sales cumulative]) - prev_sales
But this seems like more of a modeling transformation, so do it in PQ, or with a calculated column, like
current sales =
var prev = calculate( max('sales'[period]) ) - 1
var prevSales = calculate( sum(sales[sales cumulative]), all(sales), sales[period] = prev)
return
sales[sales cumulative]-prevSales

Power BI DAX Measure doesn't work with ALL() (filter unfiltered Data)

I'm pretty new here and also with Power BI and DAX. So please excuse rookie mistakes and dirty code and also bad english :P
So, I created a measure with many variables to spit out a number from 1-12 (clock).
To get to this I have to calculate in 3 different times. One current Date, one which goes back 3 quartals from the current and one which goes back 6 quartals (Current, Shortterm, Longterm). This is the setup. So I have to filter my Data first and then calculate with it.
First, I just wanted to set a Date (01.10.2021) which is fix.
But now I wanted to be dynamic. I wanted the user to choose the "current" date. Or mainly I wanted to put it in a line plot and let the visual do the slicing (with Date on the x-axis) to show development in time. But here my problem begins.
The Problem is - I guess - that I wanted Data to filter which are unaffected from extern filter (from the Visual or slicer)
I tried out the ALL() Function for the calculation so that it will not be affected from the extern slicer. The result is 0. I think I donĀ“t really understand the mechanics behind the ALL() Function and its brother and sister functions, what they do and what they don't do. The docs explanation was also not helpful. I know I did't try out many things but I sat here a few hours and really can't figure things out.
Here is a part of the DAX from the old measure which is relevant for the problem:
OLD clock=
VAR _SHORTTERM = 3
VAR _LONGTERM = 6
VAR _CURRENT_DATE = DATE(2020,10,01)
VAR _SHORT_TERM_DATE = PREVIOUSDAY(
FIRSTDATE(
DATESINPERIOD(
'Date'[Date],
_CURRENT_DATE,
-(3*_SHORTTERM),
MONTH)
))
VAR _LONG_TERM_DATE = PREVIOUSDAY(
FIRSTDATE(
DATESINPERIOD(
'Date'[Date],
_CURRENT_DATE,
-(3*_LONGTERM),
MONTH)
))
VAR _CURRENT_AVERAGE = CALCULATE(
AVERAGE(
Facts_Values[Value]),
'Facts_Values'[Quarter] == _CURRENT_DATE
)
VAR _LONGTERM_AVERAGE = CALCULATE(
AVERAGE(
Facts_Values[Value]),
'Facts_Values'[Quarter] == _LONG_TERM_DATE
)
VAR _SHORTTERM_AVERAGE = CALCULATE(
AVERAGE(
Facts_Values[Value]),
'Facts_Values'[Quarter] == _SHORT_TERM_DATE
)
VAR _LONGTERM_AVERAGE_RELATIVE = (_CURRENT_AVERAGE - _LONGTERM_AVERAGE) / (_LONGTERM - 1)
VAR _SHORTTERM_AVERAGE_RELATIVE = (_CURRENT_AVERAGE - _SHORTTERM_AVERAGE) / (_SHORTTERM - 1)
..
..
..
VAR _ALL = ROUNDDOWN((12 * (RDV+YDV+IDV+RLV+YLV+ILV)),0)+ 1 )
Return _ALL
And then my "dynamic" version
New Clock =
VAR _SHORTTERM = 3
VAR _LONGTERM = 6
VAR _CURRENT_DATE = DATE(
(YEAR(
MIN('Date'[Date]))),MONTH(MIN('Date'[Date])),01)
VAR _SHORT_TERM_DATE = PREVIOUSDAY(
FIRSTDATE(
DATESINPERIOD(
'Date'[Date],
_CURRENT_DATE,
-(3*_SHORTTERM),
MONTH)
))
VAR _LONG_TERM_DATE = PREVIOUSDAY(
FIRSTDATE(
DATESINPERIOD(
'Date'[Date],
_CURRENT_DATE,
-(3*_LONGTERM),
MONTH)
))
VAR _CURRENT_AVERAGE = CALCULATE(CALCULATE(
AVERAGEX(ALL(Facts_Values),
Facts_Values[Value])),
'Facts_Values'[Quarter from] == _CURRENT_DATE
)
VAR _LONGTERM_AVERAGE = CALCULATE(CALCULATE(
AVERAGEX(ALL(Facts_Values),
Facts_Values[Value])),
'Facts_Values'[Quarter from] == _LONG_TERM_DATE
)
VAR _SHORTTERM_AVERAGE = CALCULATE(CALCULATE(
AVERAGEX(ALL(Facts_Values),
Facts_Values[Values])),
'Facts_Values'[Quarter from] == _SHORT_TERM_DATE
)
VAR _LONGTERM_AVERAGE_RELATIVE = (_CURRENT_AVERAGE - _LONGTERM_AVERAGE) / (_LONGTERM - 1)
VAR _SHORTTERM_AVERAGE_RELATIVE = (_CURRENT_AVERAGE - _SHORTTERM_AVERAGE) / (_SHORTTERM - 1)
..
..
..
VAR _ALL = ROUNDDOWN((12 * (RDV+YDV+IDV+RLV+YLV+ILV)),0)+ 1 )
Return _ALL
Hope to find some friendly experts to solve my problem.

PowerBi Circular Dependancy issue

I have a case where I'm getting a circular dependency.
I'm trying to calculate what stock will arrive to our warehouse by a certain date, but the eta_date is a calculated date column.
The formula reporting the problem is this :
SIT Arriving Soon =
VAR PastDate = Today() - 40
VAR FutureDate = TODAY() + 14
return
CALCULATE(SUMX(OnOrder,OnOrder[Open ASN qty [units]]]), DATESBETWEEN(OnOrder[ETA Date],PastDate, FutureDate)
)
The issue is that the datesbetween function needs a column to refer to, and my column is the calculated column below, which only refers to other columns within the 'onorder' table:
to understand this formula, here is a small key:
InT is a true/false if a quantity is in transit
Open ASN qty = the qty that is in transit
I then look at different vendors and the shipping mode used to add the number of days transport time.
I then do a calculation to add to the transport time to the 'invoiced by supplier' date, and if the vendor is not one of the defined vendors, then we use the default calculated date "OnOrder[Target VSL3 Date]"
ETA Date =
Var InT = if ( OnOrder[Open ASN qty [units]]] <> 0, 1,0)
Var AAVend = if( OnOrder[Vendor] = "451633" ||
OnOrder[Vendor] = "97051583"||
OnOrder[Vendor] = "452825", "AA", "Non-AA")
Var AATransTime = if( AAVend = "AA", IF(OnOrder[Ship Mode] = "Blitz", 5, IF(OnOrder[Ship Mode] = "Air", 14, 42)), 500)
var TransTime = IF(AATransTime > 0, AATransTime, 99999)
RETURN
if ( InT = 1, DATEADD(OnOrder[Ship Date].[Date], TransTime,DAY), OnOrder[Target VSL3 Date]. [Date])
Can anyone help me to get this issue sorted, or advise a way on how I could calculate this?
Thanks very much!
Ditch the DATESBETWEEN and just filter on the date column:
SIT Arriving Soon =
VAR PastDate = Today() - 40
VAR FutureDate = TODAY() + 14
return
CALCULATE(SUM(OnOrder[Open ASN qty [units]]]), OnOrder[ETA Date] >= PastDate && OnOrder[ETA Date] <=FutureDate)
)
thanks for the reply. I tried the formula, and I still get the circular reference error. It talks about the field 'ADC invoice number'. Here is the code to that :
ADC Invoice No =
CALCULATE ( FIRSTNONBLANK ( 'ADC Movements'[ADC Invoice Number], 1 ), FILTER ( ALL ( 'ADC Movements' ), 'ADC Movements'[PoFull] = OnOrder[PoFull] ) )
I don't see how this conflicts at all.

Power BI: Calculating average of a column's current row divided by the first

Please see the file I have shared in the following link:
https://drive.google.com/file/d/1q7FE85qHoB_OhAm4hlmFidh6WP3m-UnK/view?usp=sharing
I have a dataset with the value of various items on different dates. I first need to calculate ratio by dividing the value of an item on a particular date with the earliest value in the chosen date filters. I then need to show the per day ratio of all items (columns I & J) as the desired output.
I am able to get the "first value" in the filtered table by using the following formula:
first_cr =
VAR item = MAX('Table 1'[item])
VAR min_updated = CALCULATE(
MIN('Table 1'[last_updated]),
'Table 1'[item] = item,
ALLSELECTED('Table 1')
)
RETURN
CALCULATE(
AVERAGE('Table 1'[cr]),
'Table 1'[last_updated] = min_updated,
'Table 1'[item] = item,
ALLSELECTED('Table 1'[fk])
)
However, I am unable to figure out how to calculate the average of all the individual item ratios at just the date level. I guess I need to use AVERAGEX somehow, but not sure how. Perhaps my first_cr formula could use more refinement.
I'd appreciate any help or guidance in the right direction. Thanks for your time.
I was able to get the answer using the following formula. If anyone can refine it better, please do so, else I'll accept my answer after a few days.
ret =
var lastUpdated = MAX(Sheet1[Date])
var tbl = ALLSELECTED(Sheet1)
RETURN
CALCULATE(
AVERAGEX(
Sheet1,
var i = Sheet1[Item]
var minUpdated = CALCULATE(
MIN(Sheet1[Date]),
Sheet1[Item] = i,
tbl
)
var first_cr = CALCULATE(
AVERAGE(Sheet1[Return]),
Sheet1[Date] = minUpdated,
Sheet1[Item] = i,
tbl
)
RETURN Sheet1[Return] / first_cr
),
Sheet1[Date] = lastUpdated,
tbl
)

Why does operator <= (less than or equal) not return expected result when date is variable?

Problem: DAX is not returning expected result in if statement with operator when date is a variable.
Background: I've checked to make sure that there is no date or time difference.
Dates are ALL in format (1stday of month, 12:00:00AM). I have 2 years (2018 & 2019: 24 distinct dates).
I've tried to use the <= operator in a calculated column to determine if a date is "Before/Same" to a variable date or "After". I've tried to combine < filter and = filter with || (or), same result.
TestColumn =
var CurrentDate = [Current_Period]
return
IF(
ValuesTable[Month-Year] <= CurrentDate,
"Before/Same", "After"
)
FYI: The [Current_Period] Measure, this works fine, returns one date as expected
Current_Period =
VAR ThisActMonth =
CALCULATE (
DISTINCT ( Fact_FX[Month-Year] ),
Fact_FX[bool_Latest_FX_Act] = TRUE ()
)
RETURN
ThisActMonth
I would expect that for every row where the Month-Year (Date) <= the result would be "Before/Same";
however, The result I currently get is:
"Before/Same" only where ValuesTable[Month-Year] = CurrentDate
CurrentDate = April 2019
"After" all other Months (23)
Please Help!
What's happening is that you are evaluating your Current_Period measure within the row context of the calculated column, so the measure doesn't see the row with Fact_FX[bool_Latest_FX_Act] = TRUE () except in the April row.
The solution is to calculate the Current_Period outside of that evaluation context and there are multiple possible approaches. A quick solution would be to remove row context using the ALL function inside of your CALCULATE:
Current_Period =
VAR ThisActMonth =
CALCULATE (
DISTINCT ( Fact_FX[Month-Year] ),
ALL( Fact_FX ),
Fact_FX[bool_Latest_FX_Act] = TRUE ()
)
RETURN
ThisActMonth