Types don't match between the anchor and the recursive part in column of recursive query - common-table-expression

CREATE FUNCTION [dbo].FN_GET_ALL_DATES_WITH_DAY_NAME
(
#DateFrom varchar(25)
,#DateTo varchar(25)
,#DayName varchar(25)
)
RETURNS
#ParsedList table
(
ListValue varchar(25)
)
AS
BEGIN
;WITH ALLDATES ( date )
AS
(
SELECT #DateFrom
UNION ALL
SELECT DATEADD(d,1,date)
FROM ALLDATES
WHERE date < #DateTo
)
SELECT date FROM ALLDATES WHERE DATENAME(dw, date) = #DayName
RETURN
END
I am having this error:
Msg 240, Level 16, State 1, Procedure FN_GET_ALL_DATES_WITH_DAY_NAME,
Line 18 Types don't match between the anchor and the recursive part in
column "date" of recursive query "ALLDATES".
Any help will be appreciated. Thanks.

#DateFrom is a varchar while DATEADD function returns smalldatetime.
You also try to compare the 'date' column in your CTE with #DateTo which is also a varchar.
Try explicitly matching the data types of your parameters and other columns.

Related

How to show not filtered data

I am new to power bi, and this question might be a common question and already answered, but I didn't find any precise solution.
I have two tables; "Dates" and "Accounts".
Dates table has only one column: "Date"(date type). It has only date value, which is day based.
Accounts table has two columns; Name(text type) and CreatedDate(date dypttype).
In my power bi model, there is a relationship(many to one, single, active) between Dates.Date and Accounts.CreatedDate columns.
I want to show account names that except the filtered ones. For example, my Accounts table look like this:
Name
CreatedDate
A Company
2020-01-01
B Company
2020-12-15
C Company
2021-03-03
D Company
2019-05-27
I have a slicer and use Dates.Date as It's field.
When I filtered my data as last 1 year (2020-08-18 - 2021-08-18), I want to show the data which is not filtered. I want to see this:
Name
CreatedDate
A Company
2020-01-01
D Company
2019-05-27
How to show the data which is not filtered?
You can create a filter Measure like following
_filter:=
SWITCH (
TRUE (),
/* t1 = what is value selected ?*/
CONVERT ( SELECTEDVALUE ( 'Calendar'[Calendar_Date] ), INTEGER ) = BLANK ()
/* t2=what is the max CreatedDate value visble in the filter context*/
/*if t1 and t2 both blank then return -999 else return t1-t2 which should be 0*/
&& CONVERT ( MAX ( tbl[CreatedDate] ), INTEGER ) = BLANK (), -999,
CONVERT ( SELECTEDVALUE ( 'Calendar'[Calendar_Date] ), INTEGER )
- CONVERT ( MAX ( tbl[CreatedDate] ), INTEGER )
)

Power BI if condition if true then column with date value else NULL

The below table has 2 columns
Where Column A is a Date column and Column B is a Text column where some values are equal to "x" and some are blank.
I need to create an output column which based on the below formula
IF (
AND ( ColumnA < EOMONTH ( ColumnA, 3 ), ( ColumnB = "x" ) ),
EOMONTH ( ColumnA, 3 ),
"-"
)
I have written the following DAX formula for it:
Output =
IF (
AND (
ColumnA
< EOMONTH ( DATE ( YEAR ( ColumnA ), MONTH ( ColumnA ), DAY ( ColumnA ) ), 3 ),
( ColumnB = "x" )
),
EOMONTH ( ColumnA, 3 ),
"-"
)
I'm getting an error with this formula that NULL is not allowed in this context
Note: We can leave Blank in place of "x".
How do I write the correct DAX formula to achieve the above?
The problem with your calculation is that you are mixing different data types in the same column.
The Output column is handling a date data types with a text data types, that's why you are getting an error. The columns could only handle date or text but not both at the same time.
To fix your calculation your need to change your ELSE statement from "-" to BLANK()

DAX create empty table with specific column names and no rows

How to create a table with a specified column name and no rows at all. The following oneliner does what I want but shows error message that there should be second argument in ROW function.
EmptyTable = ROW ("Product")
I would like to use it for making bridge tables with desired column name. For example I want Product_bridge table to have a column "Product".
Product_bridge = DISTINCT(
UNION(
DISTINCT( Sales[Prod_Name] )
,DISTINCT( Dictionary[Prod_DifferntName])
,DISTINCT( PriceList[P] )
))
The code above gets me the name of the first table, in this case Prod_Name.
You could just filter it. Or select TOPN 0.
TOPN:
Table = TOPN(0;DATATABLE("Product";STRING;{{}}))
FILTER:
Table = FILTER(DATATABLE("Product";STRING;{{}});0)
This is how I create empty DAX tables:
EmptyTable = DATATABLE (
"Other Measures", INTEGER,
{
{ 0 }
}
)
I would like to add to mxix answer a few useful oneliners for making one-column empty table with desired name:
OneLiner1 = TOPN(0, ROW("Product", "Apple"))
OneLiner2 = FILTER(ROW("Product", "Apple"), 1=2)
Or if you want to define column type:
OneLiner3 = TOPN(0, DATATABLE("Product", STRING,{{"Apple"}}) )
So the snipped for bridge table is:
Product_bridge = DISTINCT(
UNION(
TOPN(0, ROW("Product", "Apple"))
,DISTINCT( Sales[Prod_Name] )
,DISTINCT( Dictionary[Prod_DifferntName])
,DISTINCT( PriceList[P] )
))

TopN, Grouping, Show Others at the bottom POWERBI-DAX

I have the following formula which creates the table in the screenshot below on the left (names of actual tables are different - also it combines 2 separate tables in one) -
Top 11 Jun =
IF (
[Type Rank Jun] <= 11,
[Total Jun],
IF (
HASONEVALUE ( Partners[partner_group] ),
IF (
VALUES ( Partners[partner_group] ) = "Others",
SUMX (
FILTER ( ALL ( Partners[partner_group] ), [Type Rank Jun] > 11 ),
[Total Jun]
)
)
)
)
Now i'm stuck on how to combine the "Null" and "Others" under "Others" and put "Others" at the bottom.i can combine the "Null" & "Others" at each table level, i'm just not sure how.
The DAX solution:
To get the Other and blank (at least that is how I read your null) together, you can create a new column on the table (is easiest).
newProducts = IF(fruits[product] = BLANK(); "Other";fruits[product])
A better solution is to replace your blanks (or NULL) in the Query language:
Go to: Edit Query:
Select your table and the product column and press on the bar the "Replace values"
Do the replace and save and close the editor.
Last step
It is not relevant in which order you have the rows in the table because you can control this in the visual self.
Below example:
As you can see, I filtered other out, this is not needed when you want to count them in your top N.
If you want to show all four, we need to make a new Table:
Tabel =
var Top3 = TOPN(3;FILTER(fruits;fruits[product] <> "Other") ;fruits[July Avail])
var prioTop3 = ADDCOLUMNS(Top3;"Order"; CALCULATE(COUNTROWS(fruits);FILTER(Top3; fruits[July Avail] <= EARLIER(fruits[July Avail]))))
var Other = ADDCOLUMNS(GROUPBY(FILTER(fruits;fruits[product] = "Other");fruits[product];"June Avail"; SUMX(CURRENTGROUP();fruits[June Avail]); "July Avail"; SUMX(CURRENTGROUP();fruits[July Avail]));"Order";0)
return UNION(prioTop3; Other)
Result:

DAX to lookup date in ValidFrom column

I have two (2) tables
Tablename: X
ID Name ValidFrom Property
A-----Test1-----01.01.2010---------30
A-----Test1-----01.01.2015---------60
B-----Test1-----01.01.1900---------30
B-----Test2-----01.01.2018---------60
Tablename: Y
ID Date
A---01.01.2010
A---01.02.2010
A---01.03.2015
A---01.04.2015
Ideally, I would like to add calculated columns to Table Y which looks up ID and date with ID and ValidFrom from Table X. In this example, row#1 in Table X would be the returning row of data for all dates >= 01.01.2010 and dates < 01.01.2015. The resulting outcome would be like this:
Tablename: Y (new)
ID Date Name Property
A---01.01.2010----Test1------30
A---01.02.2010----Test1------30
A---01.03.2015----Test1------60
A---01.04.2015----Test1------60
Any help would be greatly appreciated
It's not clear how the Name column is generated, but here's how you can get the Property column once you have the Name column in table Y:
Property =
VAR LastValid =
CALCULATE (
MAX ( X[ValidFrom] ),
FILTER (
ALL ( X[ValidFrom] ),
X[ValidFrom] <= EARLIER ( Y[Date] )
)
)
RETURN
LOOKUPVALUE (
X[Property],
X[ID], Y[ID],
X[Name], Y[Name],
X[ValidFrom], LastValid
)
The LastValid variable finds the latest date that is less than or equal to the date in the current row. Then you use that along with the ID and Name to look up the Property from table X.
Taking in the last formula and applying the LOOKUPVALUE to the ID and ValidFrom as you refers, I tried successfully the next approach:
Name =
VAR LastValid =
CALCULATE(MAX(X[ValidFrom]);
FILTER (
ALL(X[ValidFrom] );
X[ValidFrom] <= EARLIER (Y[Date] )
)
)
RETURN
LOOKUPVALUE (
X[Name];
X[ID]; Y[ID];
X[ValidFrom]; LastValid
)