Function 'SAMEPERIODLASTYEAR' expects a contiguous selection Issue - visual-studio

I have a tabular data model in Visual Studio, I made a formula to get last year's sales over a given period:
Selected Measure LY:= CALCULATE([Selected Measure], SAMEPERIODLASTYEAR('date' [date]))
I made a formula to have the decomposition of all the months in the 'date' table:
FY_Month='date' [FY Month No]&'date' [Month_label]
And:
FY_Month_No=format(if('date'[month_no]>6,'date'[month_no]-6,'date'[month_no]+6), "00")
Month_label : all the months of the year
The "Selected Measure LY" display works fine when I have all months selected but when I remove one I get this error message:
Calculation error in "Selected Measure LY": the "SAMEPERIODLASTYEAR" function expects a contiguous selection when the date column comes from a tabkle on side 1 of a bidirectional relationship.
Between my 'date' table and my sales table I have a 1/N relationship, I tried to change this relationship too but the error always comes back.
I looked for solutions on forums and I found this but I don't find the same numbers as with my first formula:
Selected Measure LY:=
CALCULATE([Selected Measure],
FILTER (
ALL ( 'date' ),
YEAR ( 'date'[date] ) = YEAR ( TODAY () )
&& 'date'[date] <= TODAY ()
)
)
My code for Selected Measure:
Selected Measure:=
VAR hasFilter =
HASONEFILTER ( 'Measure Selection'[Measure Name] )
VAR selMeas =
SELECTEDVALUE ( 'Measure Selection'[Measure Name], BLANK () ) /*SWITCH works slow, so using many IFs*/
VAR preCalc =
IF (
selMeas = "Units",
[Sku Piece Quantity],
IF (
selMeas = "Net Sales",
[Euro Net Amount],
IF (
selMeas = "Gross Sales",
[Euro Gross Amount],
IF (
selMeas = "Average Wholesale",
[Average_Wholesale],
IF (
selMeas = "Average Units",
[Average Units],
IF (
selMeas = "No of Styles",
[Style Count],
IF (
selMeas = "No of Colours",
[Colour Count],
IF (
selMeas = "ROD by Style",
[ROD by Style],
IF (
selMeas = "Standard Cost",
[Sum_Cost_Base],
IF (
selMeas = "Net Margin",
[Net_Margin],
IF (
selMeas = "Gross Margin",
[Gross_Margin],
IF ( selMeas = "ROD by Colour", [ROD by Colour], BLANK () )
)
)
)
)
)
)
)
)
)
)
)
VAR calc =
IF ( hasFilter, preCalc, BLANK () )
RETURN
calc
How can I correct it?

Related

Grand Total is incorrect is SSAS Tabular Model

The number I see in the grand total is different from the sum of the rows (for previous year column [Önceki Yıl IPP]).
Table columns
Measures:
IPP Ciro:=SUM([Amount_])/1000
This measure calculates sum of revenue(Amount_)
Önceki Yıl IPP:=
VAR LastDaySelectionIPP =
LASTNONBLANK ( Tarih[Tarih], [IPP Ciro] )
VAR CurrentRangeIPP =
DATESBETWEEN ( Tarih[Tarih], MIN ( Tarih[Tarih] ), LastDaySelectionIPP )
VAR PreviousRange =
SAMEPERIODLASTYEAR ( CurrentRangeIPP )
RETURN
IF (
LastDaySelectionIPP >= MIN ( Tarih[Tarih] ),
CALCULATE ( [IPP Ciro], PreviousRange )
)
This measure calculates sum of previous year revenue.
I want to compare the current year with the last year same period.
Why is the Grand total is 98.998 for previous year[Önceki Yıl IPP]?
Grand Total is incorrect is SSAS Tabular Model
It’s not jumping out at me, but here’s how to debug it.
Add a new measure like this and add it to your table:
Debug:=
VAR LastDaySelectionIPP =
LASTNONBLANK ( Tarih[Tarih], [IPP Ciro] )
VAR MinDate = MIN ( Tarih[Tarih] )
VAR CurrentRangeIPP =
DATESBETWEEN ( Tarih[Tarih], MIN ( Tarih[Tarih] ), LastDaySelectionIPP )
VAR PreviousRange =
SAMEPERIODLASTYEAR ( CurrentRangeIPP )
VAR WrongAnswer =
IF (
LastDaySelectionIPP >= MIN ( Tarih[Tarih] ),
CALCULATE ( [IPP Ciro], PreviousRange )
)
RETURN LastDaySelectionIPP & “,” & MinDate & “,” & [IPP Ciro] & “,” & WrongAnswer
It will list the values you are using in the calculation and I predict you’ll find the unexpected value. Update us on what you find!

DAX GENERATE & ALLNOBLANKROW functions lead to circular dependency

I'm trying to use a DAX function to generate a table in Power BI. I have a fact table with Opened & Closed date columns and there is a requirement to report at the end of each day/month/year how many items were backlogged.
I've got the table to generate successfully with the code below - essentially joining the date and fact tables, however I can't then link it back to my dimensions due to a circular dependency error.
Researching it online suggests that I need to remove the blank row from fact_task_transaction with the ALLNOBLANKROW function. Unfortunately this has no effect.
Can anyone help?
Backlog Per Day =
var res = SELECTCOLUMNS (
GENERATE (
fact_task_transaction,
FILTER (
ALLNOBLANKROW ( 'Date' ),
AND(
'Date'[Date] >= fact_task_transaction[Opened At Date],
'Date'[Date] <= fact_task_transaction[Closed At Date]
)
)
),
"Date", 'Date'[Date],
"Task ID", fact_task_transaction[Task Id],
"Assignee ID", fact_task_transaction[Assignee Id]
)
return res
try this code - it only uses fact_task_transaction, so the joins with dimensions shuld be working fine
Backlog Per Day =
SELECTCOLUMNS (
GENERATE (
'fact_task_transaction',
GENERATESERIES (
CALCULATE ( MIN ( 'fact_task_transaction'[Opened At Date] ) ),
CALCULATE ( MAX ( 'fact_task_transaction'[Closed At Date] ) ),
1
)
),
"Date", [Value],
"Task ID", fact_task_transaction[Task Id],
"Assignee ID", fact_task_transaction[Assignee Id]
)

Based on a value show different message

Follow Up :
I have these two tables that are mutually exclusive (not connected in any way) .
The first table has date , number of customers on the dayDISTINCTCOUNT(sales[user_name]), total sales , tier (text - will explain)
The second table is CustomerLimit which is basically consecutive numbers between 1 and 100.
Used the tier measure as the answer below (thank you)
Tier =
VAR Limit = SELECTEDVALUE ( CustomerLimit[CustomerLimit] )
VAR CustCount = COUNT ( Customers[CustomerID] )
RETURN
IF (
ISBLANK ( Limit ), "Select a value",
IF ( CustCount > Limit, "Good", "Bad" )
)
Now I need to aggregate the total amount of customers by Tier.
I used
calculate(DISTINCTCOUNT(sales[user_name]),Tier = "Good") .
It give me an error of : A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.
Is that possible ?
You can capture the limit using SELECTEDVALUE and then compare.
Tier =
VAR Limit = SELECTEDVALUE ( CustomerLimit[CustomerLimit] )
VAR CustCount = COUNT ( Customers[CustomerID] )
RETURN
IF (
ISBLANK ( Limit ), "Select a value",
IF ( CustCount > Limit, "Good", "Bad" )
)

How to get Closing Inventory Qty right - DAX

I can't find the right results for Closing Inventory, given the scenario below:
Measure added to Matrix Table:
Calculation=
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
RETURN
SWITCH (
SELECTEDVALUE ( HelperTable[ID] ),
1, IF ( curDate = minDate, [OpenInventoryMeasure], BLANK () ),
2, IF ( curDate > minDate, [ProdMeasure], BLANK () ),
3, [QtySalesMeasure],
4, if (curDate > minDate,[Closing Inventory], BLANK())
)
Here are the measures created and used in the main calculation above:
OpenInventoryMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
return
CALCULATE(Sum('Production'[Qty]),FILTER('Calendar','Calendar'[Date] = minDate))
ProdMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
return
CALCULATE(Sum('Production'[Value]),FILTER('Calendar','Calendar'[Date] > minDate))
QtySalesMeasure = SUM(PedItens[QTDUND])
ClosingMeasure = [OpenInventoryMeasure ] + [ProdMeasure] - [QtySalesMeasure]
Main issue: Right now, Closing measure is not giving me the right result, although the measure that make it up are correct.
Tiny issue: Also, the order is not right in the table, although I had ordered it by ID.
Here's a screenshot of the resulting table:
I appreciate any help.
The problem is with the Opening Inventory measure, because when you do the CALCULATE you are now on the context of the Monday, so it's already filtered by Monday and then you're adding an additional filter of date = 7th which returns blank.
You need to clear the filters first and then apply the date filter:
OpenInventoryMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
return
CALCULATE(Sum('Production'[Qty]), ALLSELECTED('Calendar'), 'Calendar'[Date] = minDate)
Your Prod measure probably suffers from the same error, but I don't understand what's the logic behind it because you only filter based on min date and not on current date.

Count unique matching items with filter as a calculated column

I have two tables are Data and Report.
Data Table:
In Data table contain three columns are Item, status, and filter.
The item contains duplicated entry and the item column contains text and number or number only or text only.
The status column contains two different text/comments, "Okay" and "Not Okay"
The filter column contains two different filters which are A1 and A2.
The report table
In the Report table, I updated both comments/text as "Okay" or "Not Okay". I am looking for count against filter A1 and A2 according to the comments.
I would like to create a new calculated column in the report table in order to get the unique count according to the comments and filter based on the data table columns item and status.
DATA:
REPORT
Alexis Olson helped the following calculated column in order to get the unique count. I am trying to add one more filter in existing DAX calculated column but it's not working. Can you please advise?
1.Desired Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment
)
2.Desired Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] ),
DATA[ITEM]
)
)
3.Desired Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF ( CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS], 1, 0 )
)
I think you can just add a filter to CALCULATE:
Filter by A1 Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment,
DATA[FILTER] = "A1"
)
For the second method,
Filter by A1 Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] && REPORT[FILTER] = "A1" ),
DATA[ITEM]
)
)
I do not recommend using the third one but it would be like this
Filter by A1 Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF (
CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS]
&& CALCULATE ( SELECTEDVALUE ( DATA[FILTER] ) ) = "A1",
1,
0
)
)

Resources