Power bi How can i display items without data with a zero AND hide the months in the future - powerbi-desktop

I'm using the following measure in order to make the charts display the periods that don't have a value. But the problem now is that it is displaying the months in the future.
x Count Tickets =
IF
(
ISBLANK
(
DISTINCTCOUNT('Tickets DB'[Número de Ticket])
)
,0,
DISTINCTCOUNT('Tickets DB'[Número de Ticket])
)
How can I avoid the months in the future from displaying?
I also tried the following measure:
x Count Tickets =
IF
(
DATEDIFF(MAX('Tickets DB'[Fecha]),TODAY(),DAY)>0,
IF
(
ISBLANK
(
DISTINCTCOUNT('Tickets DB'[Número de Ticket])
)
,0,
DISTINCTCOUNT('Tickets DB'[Número de Ticket])
)
)
But it is not displaying months without data
Thanks in advance.

1. First create a calculated column(calendar dimension table), to return if there is a record on all dates of this dimension
example of calculated column
2.
Create the measurement below (
Where you will change the context of the measurement to bring only the true fields from the column you created earlier)
Medida Stack =
CALCULATE(
DISTINCTCOUNT(
fVendas[CLIENTE/LOJA]);
CALCULATETABLE(
dCalendario;
dCalendario[Has Sales?]=TRUE()
)
)
fvendas[Cliente/loja] = 'Tickets DB'[Número de Ticket]
dCalendario = calendar dimension table

If it is always the dates in the future you want to hide, you could opt for relative date filtering on your visual. More info here

IF (
MAX ( 'DimDate'[Date] ) > TODAY (),
BLANK (),
DISTINCTCOUNT ( 'Tickets DB'[Número de Ticket] ) + 0
)
Starting from your original measure, we check whether the date in context is after today's date. In that case, we blank the measure. Otherwise we do your DISTINCTCOUNT, which may possibly return a blank value. Note that DAX blanks are not equivalent to SQL NULLs - specifically addition with a blank returns a number, unless both addends are blank; so BLANK () + 0 = 0. This neatly solves your display for months with no data. But we only execute this logic for past months.
Note, I've written this assuming that you have a date table. If you don't, you should get one. I like mine, but there are plenty of good ones out there.

Create a calendar table using something like the following:
Calendar =
VAR MinDate = MIN('Tickets DB'[Fecha])
VAR MaxDate = MAX('Tickets DB'[Fecha])
VAR BaseCalendar =
CALENDAR ( MinDate, MaxDate )
RETURN
GENERATE (
BaseCalendar,
VAR Basedate = [Date]
VAR YearDate =
YEAR ( Basedate )
VAR MonthNumber =
MONTH ( Basedate )
VAR YrMonth = 100 * YearDate + MonthNumber
VAR Qtr =
CONCATENATE ( "Q", CEILING ( MONTH ( Basedate ) / 3, 1 ) )
VAR QtrYr = YearDate & " " & Qtr
RETURN
ROW (
"Day", Basedate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( Basedate, "mmmm" ),
"Year Month", FORMAT ( Basedate, "mmm yy" ),
"YrMonth", YrMonth,
"Qtr", Qtr,
"QtrYr", QtrYr
)
)
If you have dates in the future in your table, replace the MAX('Tickets DB'[Fecha]) part to NOW() then just create a relationship from your 'Tickets DB' table to this calendar table on both date fields and that should correct the issue (and allow you to use time intelligence functions). Once this is set up correctly your measure should work just fine.
Hope it helps.

Related

Excel Power Pivot - Nested IF OR Formula error

I have been trying to use the formula mentioned below which is not working and showing an error message stating "Power pivot expression that yield variant data-type cannot be used to define calculated columns"
IF (
OR ( Table2[TeamStatus] = "Lost", Table2[TeamStatus] = "Won't proceed" ),
"0",
IF (
OR ( Table2[TeamStatus] = "Next level", Table2[TeamStatus] = "Disqualified" ),
"0",
IF (
AND ( YEAR( Table2[Match Date] ) = YEAR( TODAY() ), [TeamStatus] <> "Won" ),
[Spent Amount],
0
)
)
)
Data types of the columns
1) [TeamStatus] = Text
2) [Match Date] = Date
3) [Spent amount] = Number
The purpose of this formula is to exclude the following rows from [TeamStatus] column (i.e. in the [TeamStatus] column is having "Lost", "Won't proceed", "Next Level", "Disqualified" texts) then the output should be zero and then calculate the spent amount for all other categories in [TeamStatus] column and another condition (i.e. if the year is current year) is met.
I have been trying from past 1 day to make this formula work and no luck. I also tried using FORMAT() function and still it is not working.
Can someone please help me?
The error is saying that your formula should either return a number or text but not sometimes a number and sometimes text.
Notice that your formula sometimes returns "0" and sometimes returns 0.
Either change the first two zeros to be numbers instead of text or else change the last zero to text and use FORMAT on [Spent Amount] to convert it to text.
As a side note, can't your formula be simplified to just?
IF (
AND ( YEAR ( Table2[Match Date] ) = YEAR ( TODAY () ), [TeamStatus] = "Won" ),
[Spent Amount],
0
)

DAX sum of multiplication (sumproduct) across multiple tables

I am struggling with what I think is a really silly problem.
I am trying to get "MTD target" values from an arbitrary date selection. I have a table 'out' which has many dimensions +date, as well as a target table which gives me a constant target value per day, for each month. My goal is to get the number of days selected per EACH month and multiply it by the corresponding daily target for the relevant month.
For example, month 1 daily target is 2, month 2 daily target is 4. I have 4 days in month 1, and 3 days in month 2. My cumulative target should be 2*4+3*2 = 14.
I can do this for a single month no problem. But as soon as I have a date range selected that crosses 2 or more months it all goes to hell.
Table 'out' has date, country, and other dimensions.
Table 'targets' has a year, month, and country dimensions.
I am trying to get some join and multiplication that is something like SUM(month_country * selected_days)
Here are the DAX measures I've tried:
mtd_inv_tgt :=
CALCULATE (
SUM ( targets[daily_spend] ),
FILTER (
targets,
targets[market] = FIRSTNONBLANK ( out[co_market], "" )
&& targets[yyyymm] >= MIN ( out[yyyymm] )
&& targets[yyyymm] <= MAX ( out[yyyymm] )
)
)
* DISTINCTCOUNT ( out[date] )
mtd_inv_tgt :=
SUMX (
FILTER (
targets,
targets[market] = FIRSTNONBLANK ( out[co_market], "" )
&& targets[yyyymm] >= MIN ( out[yyyymm] )
&& targets[yyyymm] <= MAX ( out[yyyymm] )
),
targets[daily_spend] * DISTINCTCOUNT ( out[date] )
)
This works fine if the dates selected belong to one month. If I select 2 months it will add the daily spend across 2 months and then multiply it by the number of dates covering the 2 months. Like from the above example it would be (2+3)*(4+2) = 30, which is obviously wrong.
The caveat is I can't use SUMX on the 'out' table because there are many records per date+country, whilst the targets are a single entry per month+country.
I think something similar to this should work:
mtd_inv_tgt :=
SUMX (
VALUES ( out[date] ),
LOOKUPVALUE (
targets[daily_spend],
targets[yyyymm], SELECTEDVALUE ( out[yyymm] )
)
)
This iterates over each distinct out[date] value in the current filter context and adds up the daily_spend for each of those dates that it looks up by matching on yyymm.
Iterating over only the yyymm might be more efficient:
mtd_inv_tgt :=
SUMX (
VALUES ( out[yyymm] ),
DISTINCTCOUNT ( out[date] )
* LOOKUPVALUE (
targets[daily_spend],
targets[yyyymm], EARLIER ( out[yyymm] )
)
)
Note: If these don't work as expected, please provide sample data to check against as I haven't actually tested these.

Show year to date sales for previous years

I am making a report that counts the amounts of offers, uniquely identified with SALE_ID, containing data from different products starting first of January 2015 ranging up to todays date (18/12/2017 at the time of asking). I am counting the amounts of offers with a simple measure called 'Distinct':
Distinct := DISTINCTCOUNT(dOffers[Sale_ID])
This gives me satisfactory results, as in, I am receiving the right counts for the considered period. I am also calculating year-over-year changes, defining the previous year offers with the following measure: (dCalendar contains the datekey table).
PY Offers :=
SUMX (
VALUES ( dCalender[YearMonthNumber] );
IF (
CALCULATE ( COUNTROWS ( VALUES ( dCalender[FullDates] ) ) )
= CALCULATE ( VALUES ( dCalender[MonthDays] ) );
CALCULATE (
[Distinct];
ALL ( dCalender );
FILTER (
ALL ( dCalender[YearMonthNumber] );
dCalender[YearMonthNumber]
= EARLIER ( dCalender[YearMonthNumber] ) - 12
)
);
CALCULATE (
[Distinct];
ALL ( dCalender );
CALCULATETABLE ( VALUES ( dCalender[MonthDayNumber] ) );
FILTER (
ALL ( dCalender[YearMonthNumber] );
dCalender[YearMonthNumber]
= EARLIER ( dCalender[YearMonthNumber] ) - 12
)
)
)
)
The problem that I am having, is that the year-over-year change for the month december (the running month), considers the year-to-date sales for this year (2017) and compares this to full month sales in the previous years (2016 and 2015); this makes the last months comparison uninterpretable, as we are comparing offers from half a month to offers from a full month.
I would like to know how to fix this problem: i.e. consider the sales for the full year up to todays date, and compare this for the exact same periods last year and two years ago (2015: start Jan 1st and go up to Dec 18th; idem dito for 2016 and 2017). The SAMEPERIODLASTYEAR call might seem straightforward forthis issue, but I am receiving a contiguous dates errors...
Thanks in advance!
From your description, I understand that you're trying to perform year over year month to date comparisons.
Without relying on any time intelligence functions like SAMEPERIODLASTYEAR, you may want to try this version of the measure:
PY Offers :=
SUMX(
VALUES( dCalendar[YearMonthNumber] );
CALCULATE(
VAR currDate = MAX( dCalendar[FullDates] )
VAR currYear = YEAR(currDate)
VAR currMonth = MONTH(currDate)
VAR currDay = DAY(currDate)
RETURN
CALCULATE(
[Distinct];
ALL( dCalendar );
YEAR(dCalendar[FullDates]) = currYear - 1;
MONTH(dCalendar[FullDates]) = currMonth;
DAY(dCalendar[FullDates]) <= currDay
)
)
)

Rolling 12 Month sum in PowerPivot

In PowerPivot Excel 2016 I write a formula for rolling 12 month sum of sales as below :
Rolling Sum:=CALCULATE (
[Sales] ,
DATESBETWEEN (
Sales[Date],
FIRSTDATE(DATEADD(Sales[Date],-365,DAY)),
LASTDATE (Sales[Date] )
)
)
But it seems not working correctly. for each month it shows me only sales of that month!
Does anybody knows how should I fix my problem?!
Thanks In Advance
If you don't have a Date/Calendar table you can't use Time Intelligence functions properly.
Despite the best practice would be have a Calendar/Date table and use Time Intelligence functions, you can get the desired result by using an explicit filter:
Rolling Sum :=
CALCULATE (
[Sales],
FILTER (
ALL ( Sales ),
[Date]
>= MAX ( Sales[Date] ) - 365
&& [Date] <= MAX ( Sales[Date] )
)
)
Let me know if this helps.
Using fixed 365 days does not properly account for leap days. In the following code, "[CurDatePerCalendar]" measure contains the date for which you want to calculate the average.
Sales TTM =
VAR base = [CurDatePerCalendar]
VAR StartDate = EDATE(base,-12) + 1
VAR EndDate = base
RETURN
CALCULATE(
[SalesSum],
FILTER ( _Calendar,
_Calendar[Date] >= StartDate
&&
_Calendar[Date] <= EndDate )
)

DAX Time Intelligence with Conditional Logic

I have a "sales" table with team ID, payment type and date (see example below):
I want to write a DAX formula that will "lookup" the prior month's sales amount for a "Monthly" team and the 3 month prior sales amount for a "Quarterly" team. The lookup also needs to be filtered by team ID, so the prior sales belong to the same team.
Here's an example of the desired output (Change in Sales) :
PriorMonthSales =CALCULATE(
SUM(Table[Sales]),
FILTER(
Table[TeamID]&&
Table[Date] - 1
)
PriorQuarterSales =CALCULATE(
SUM(Table[Sales]),
FILTER(
Table[TeamID]&&
Table[Date] - 3
)
PriorSales = IF(Table[PaymentType] = "Monthly",PriorMonthSales, PriorQuarterSales)
These formulas obviously don't produce the desired result, but I am sharing them here to show my approach to solving this. Perhaps, someone more knowledgeable here can assist with the correct syntax and logic.
Thanks!
To calculate the total of the previous month/quarter you can add a calculated column. However you need to change your month column to a date.
jan 16 => 1/1/2016
formula:
=
IF (
myTable[paymenttype] = "monthly",
CALCULATE (
SUM ( myTable[sales] ),
FILTER (
myTable,
myTable[TeamID] = EARLIER ( myTable[TeamID] )
&& DATEADD ( myTable[date], 1, MONTH ) = EARLIER ( myTable[date] )
&& myTable[paymenttype] = EARLIER ( myTable[paymenttype] )
)
),
CALCULATE (
SUM ( myTable[sales] ),
FILTER (
myTable,
myTable[TeamID] = EARLIER ( myTable[TeamID] )
&& DATEADD ( myTable[date], 3, MONTH ) = EARLIER ( myTable[date] )
&& myTable[paymenttype] = EARLIER ( myTable[paymenttype] )
)
)
)

Resources