IF statement comparing the dates from same column - powerbi

I have sql query like below : (have put only skeleton)
select
e1.empname,
case
when e1.date in {"20201001","20200102"}
and e2.date not in {"20200101","20200102","20200103"} then "entry"
when v1.date > v2.date then "exit"
when v1.date < v2.date then "detsined"
else "promoted"
end as "As usual"
from employee v1, employee v2
where v1.quarter= "2015 Q3"
I'm trying to write a equivalent dax measure. IF(table[datestring]>table[datestrin]) this is not working . I understand that we need to use the self join concept here. Can someone help me out here?

The feature is not available on power bi

Related

Power BI - Use Column name to create Row and attach Value from that Row to it

I have a table from a survey that reports the score given to a specific employee, and various columns are there to hold each score for each question. Like this table below:
Now, what I want to do is make a row for each question, and in the original table, each question is a column. And I'd like for example, John, to have 1 entry for each question, and the average of that score stored next to each question like in this table here:
.
This shows clearly what I'm aiming for.
I believe I need some sort of pivot or unpivot table going on, but I'm not too sure on the Power BI DAX syntax for creating a new table.
I currently have a table that provides each Employee once, and columns showing their average score for each question, but that is a bit harder to dice up the way I want to. Code pasted below:
ReportTable =
SUMMARIZE(
ALL ( '360Sample'[Name], '360Sample'[Relationship to person being reviewed]),
'360Sample'[Name],
'360Sample'[Relationship to person being reviewed],
"Employee Satisfaction", DIVIDE(CALCULATE(AVERAGE('360Sample'[Treats others with respect/Truly values employees]))+AVERAGE('360Sample'[Encourages and supports staff in developing their skills])+AVERAGE('360Sample'[Provides effective mentoring]),3),
"Quality Product", DIVIDE(CALCULATE(AVERAGE('360Sample'[Consistently strives to provide products above industry quality standards]))+AVERAGE('360Sample'[Takes ownership of project outcome])+AVERAGE('360Sample'[Ensures quality control always happens on products]),3),
"Client Satisfaction", DIVIDE(CALCULATE(AVERAGE('360Sample'[Fosters open, honest, and consistent communication with clients]))+AVERAGE('360Sample'[Responds quickly to client questions and concerns.])+AVERAGE('360Sample'[Successfully communicates contractual needs and requirements with the Client, including schedules and scope and fee increases]),3),
)
Only difference here is that I have an extra attribute of "Relationship" which I'll also need to include but is less important for now. It makes a row of each employee for every unique Relationship, which is 2.
Hello You need to first use the "unpivot" in Power Query to convert your table into this shape: It is not so hard.
Like this:
Then use this DAX Code:
ReportTable =
ADDCOLUMNS (
SUMMARIZE ( 360Sample, 360Sample[Name], 360Sample[ScoreNum] ),
"ScoreAvg", CALCULATE ( AVERAGE ( 360Sample[Score] ) )
)
And It produces:

List Dates and customizing tables in Power Query

I'm trying to Add Custom Column in Power Query with the objective to return a Table from a List of dates.
The syntax used is as follows below:
= Table.AddColumn(TypeDate, "AddTable", each Table.FromList(
List.Dates([Date_begin],1,#duration(1,0,0,0)
)))
where:
TypeDate is the name of last step in Power Query
"AddTable" is the name of added custom column
[Date_begin] is a column with dates to be considered as the start of my list
Although the syntax seems correct, Power Query returns an error described as follows:
Expression.Error: We could not convert the value #date(2021, 1, 1) on to Text.
Details:
Value=01/01/2021
Type=[Type]
Does anyone know how to handle this problem?
I'll show an image where Power Query shows the error.
Select here to see Power Query interface
Your question is unclear
You want to add a column that has a table of dates for each row, using Date_Begin and Mes_Final?
#"Added Custom" = Table.AddColumn(TypeDate, "AddTable", each Table.TransformColumnTypes(Table.FromList({Number.From([Date_Begin])..Number.From([Mes_Final])}, Splitter.SplitByNothing(), {"date"}),{{"date", type date}}))

Looking for a best way to convert complex SQL filter in Power BI

I'm looking for a best way to implement a complex SQL filter in a Power BI report.
The filter is presently implemented in a database view fact table (SQL database), that is used as a data source for a corresponding model table. The filter condition is to be removed from this database view and is to implemented in Power BI report.
Here is the filter:-
WHERE X_FLAG = 'N'
AND DIM1.ATTR1 IN (VAL1, VAL2, VAL3 ...ETC)
AND DIM2.ATTR2 IN (VAL11, VAL22, VAL33 ..ETC)
AND DIM3.ATTR3 >= 101010 AND DIM3.ATTR3 <= 919191
AND (CASE WHEN DIM1 IN (VAL1) AND DATE_DIM >= SOME_DATE_VAL THEN
CASE WHEN DIM2.ATTR3 = 874 AND FCT_LOC_CODE='123'
...FEW MORE CONDITION THEN 0 ELSE 1 END
ELSE 1 END ) = 1
So, this doesn't looks like easy.
The report connects to a SSAS model (the tabular model has all these dims and fact related).
One way I can think of is to implemented this logic somewhere in the tabular model as a flag and use this flag in the Power BI report. Now when the users need to have the data filtered by this logic they can use this flag in filter section, if not they can remove this attribute.
Are there any other ways to implement such a thing which would be more flexible for the end users?

Power BI - Creating a calculated table

I am creating a dashboard in Power BI. I have to report the executions of a process in a daily basis. When selecting one of these days, I want to create another calculated table based on the day selected (providing concrete information about the number of executions and hours) as it follows:
TABLE_B = FILTER(TABLE_A; TABLE_A[EXEC_DATE] = [dateSelected])
When [dateSelected] is previously calculated from the selected day as it follows:
dateSelected = FORMAT(FIRSTDATE(TABLE_A[EXEC_DATE]);"dd/MM/yyyy")
I tried a lot of alternatives as, for example, create individualy the year, month and day to later compare. I used the format in both sides of the comparation, but none of them works for me. The most of the cases it returns me the whole source table without any kind of filters. In other cases, it doesn't return anything. But, when I put a concrete day ...
TABLE_B = FILTER(TABLE_A; TABLE_A[EXEC_DATE] = "20/02/2019")
... it makes the filter correctly generating the table as I want.
Does someone know how to implement the functionality I am searching for?
Thanks in advance.
You're almost there Juan. You simply need to use dateSelected as a varialbe inside of your DAX query:
TABLE_B =
var dateSelected = FIRSTDATE(TABLE_A[EXEC_DATE])
return
FILTER(TABLE_A, TABLE_A[EXEC_DATE] = dateSelected)
Note that all my dates are formatted as Date so I didn't need to use a FORMAT function.
Here's the final result:
I admit that this behavior can be quite confusing! Here is a useful link that will help you understand Power BI's context:
https://community.powerbi.com/t5/Desktop/Filtering-table-by-measures/td-p/131361
Let's treat option 1 as FILTER(TABLE_A; TABLE_A[EXEC_DATE] = "20/02/2019") and option 2 as FILTER(TABLE_A; TABLE_A[EXEC_DATE] = [dateSelected]). Quote from the post:
In option 1, in the filter function, you are iterating
over each row of your 'Table' (row context). In option 2, because you
are using a measure as part of the filter condition, this row context
is transformed into an equivalent filter context (context transition).
Using variables (...) is very convenient when you want to filter
a column based on the value of a measure but you don't want context
transition to apply.

SQL Update table1.column1 if table1.column2 like table2.column2

I have two tables. Products P1 and Manufacturers M1. I want to see if the manufacturer_name is in the P1.title and update the P1.manufacturer_id with the M1.manufacturer_id.
UPDATE products P1
set P1.manufacturer_id=manufacturers.manufacturer_id
WHERE P1.title LIKE manufacturers.manufacturer_name;
I am getting Error "#1054 - Unknown column 'manufacturers.manufacturer_name' in 'where clause' "
Also, what is the correct syntax for WHERE P1.title LIKE %M1.manufacturer_name%?
Thank you.
You didn't post much about table structure, or the relationship between manufacturer and products, so the below is my best guess: please review and test it thoroughly. I'd prefer to write this using a JOIN, but without knowing what the foreign keys are, I'm not willing to take the chance on duplication ...
UPDATE P1
SET P1.Manufacturer_ID =
(
SELECT TOP 1 Manufacturer_ID
FROM Manufacturers
WHERE P1.Title LIKE '%' + manufacturer_name + '%'
)
FROM Products P1