I have 3 columns where there will only ever be a value within one of the three columns. I'm trying to get that value into a single column to make the reporting easier.
Can anyone see where I'm going from with what I have below?
WhichForm = SWITCH (
TRUE,
ISBLANK([Column 1]) = FALSE, [Column 1],
ISBLANK([Column 2]) = FALSE, [Column 2],
ISBLANK([Column 3]) = FALSE, [Column 3]
)
This should be done in Power Query but if you want to use DAX, use the COALESCE() function.
https://learn.microsoft.com/en-us/dax/coalesce-function-dax
Related
I'm analysing some data using Power BI, and I have a field in the data called deflection. Normally the value in the field is 0, but a small number of results will have other values ranging from -360 to +360.
Within the dataset, any value > 2 or < -2 might be significant.
I would like to create a new boolean value to indicate if the deflection value is significant or not.
Any value between -2 and + 2 is NOT significant - bSigDeflection = False
Any other value - bSigDeflection = true
How do I approach this?
I have started by using an abs(deflection) value in the sql query, just to keep everything positive. I could write a sql CASE statement that would probably work but I feel that there must be a simpler way to achieve this in Power BI that I am missing.
Any suggestions, clever people?
Thanks
You may use TransformColumns to replace the value with the bool. Or you can use the same code with Table.AddColumn if you want to keep the original value.
let
Source = ...,
coerceDefelectBool = (input) as logical =>
if input < 2 or input > 2 then true else false,
Final = Table.TransformColumns(
Source,
{"Deflection", coerceDefelectBool},
null, MissingField.Ignore
)
in
Final
If you wanted a measure, not a DAX calculated column, you can try something like this.
[ bSigDeflection ] = SWITCH (
TRUE,
SELECTEDVALUE( [Table].[Deflection] ) > 2, TRUE(),
SELECTEDVALUE( [Table].[Deflection] ) < 2, TRUE(),
FALSE()
)
I would like my SQL query in PBI to be defined by parameter. The Parameter1 is a list of two values Big and Small. If I set up Parameter1 to Small then I want only a sample of data to be loaded. I am doing it this way:
let
ReturnQueryVar = (optional x) =>
if Parameter1 = "Small"
then " and Product in (1, 2, 3)"
else "",
Source = Sql.Database(
"myservername",
"mydatabase",
[
Query = "
select *
from table
where
1=1"
& ReturnQuery()
]
)
in
Source
I get this error:
Expression.Error: The name 'ReturnQuery' wasn't recognized. Make sure it's spelled correctly.
Update.
Facepalm! It was a typo. ReturnQuery should be ReturnQueryVar.
However, I would like to leave the question open for the part:
ReturnQueryVar = (optional x) =>
if Parameter1 = "Small"
then " and Product in (1, 2, 3)"
else ""
Does PowerQuery have a syntax for IF:
TextVar = IF(Parameter1 = 'S', 'This', 'That' )
I haven't seen syntax like that, but if you like you can create a function that does that, basically a "container" for the IF statement of Power Query.
For example, you can create a Blank Query called IF with the following contents.
(cond as logical, if_true as any, if_false as any ) =>
if cond then if_true else if_false
Using that function, anywhere it would have similar results and an IF statement.
I am just sharing here a PowerQuery snippet I ended up with. This code allows you to choose with a parameter between two alternate queries - the first for a sample and the other for the full rowset. It allows to download only a small sample of rows from a large table into Power BI Desktop.
let
QueryCode = if Query_Parameter = "Sample"
then
"select 1"
else
"select 2",
Source = Sql.Database(
"MyServer",
"MyDataBase",
[ Query = QueryCode ]
)
in
Source
Prior to that in Power Query Editor > Manage Parameters > Add New Parameter named Query_Parameter with 2 text values as a list: "Sample" and "Full" and based on this parameter the source of the query changes. Just replace "select 1" with anything narrowing your rows like "select top 1000..." or add WHERE condition. "select 2" is your query with full row set.
i want avoid using queryset inside loop
cuz that hit database a lot
i have to list
Degreyid: [1, 3, 2, 3]
companyid: [2, 2, 2, 1]
i want use filtering in conjunction:
-i want filter for object have Degreyid:1 and companyid:2 and seconde test should be Degreyid:3 and companyid:2.......... Degreyid:3 and companyid:1 .
i dont want use loop like this :
i=0
list=[]
while i < len(listilam):
ddegrycomp = DegreyCompany.objects.filter(withdegrey=Degreyid[i], company=companyid[i])
i+=1
there is any way to use filter with two list in parallel ??
note:
using : .filter(withdegrey__in=Degreyid, company__in=companyid) dont help here
You can retrieve the elements with:
from django.db.models import Q
q_filter = Q(
*[Q(withdegrey=x, company=y)
for x, y in zip(Degreyid, companyid)],
_connector=Q.OR
)
items = DegreyCompany.objects.filter(q_filter)
Here we construct a Q object. This is a filter expression, but we do not query with the loop. Then we thus retrieve the elements in one single query.
I'd like to ask about a problem related to regexp_extract in Hive. So, I'd like extract a value from json object
{"data":[{"key":"text","value":"12345"},{"key2":"text2","value":""}],"key3":time1,"key4":time2}
I use the regexp_extract query like this:
max(if(index=0, regexp_extract(column1, '"key":"text","value":"(.+?)"', 1), '')) as extract1,
max(if(index=1, regexp_extract(column1, '"key":"text","value":"(.+?)"', 1), '')) as extract2
but I got duplicate data for each, so let say it should return only 2 rows (row 1, row 2) but it gave me 4 rows instead (row 1, row 1, row 2, row 2) with completely identical data.
However, I've solved this by changing the regexp_extract into get_json_object like this:
max(if(index = 0, get_json_object(column1, '$.data[0].value'), '')) as extract_1,
max(if(index = 1, get_json_object(column1, '$.data[0].value'), '')) as extract_2
but still couldn't find the reason. Does anyone know why? Really appreciate your help!
Explanation
The way this spreadsheet should work is by checking if the Status value of the last corresponding Data value, is equal to "OK". If it is, then it'll show the corresponding value in ValueIfOK, in Result, if not, ValueIfOK will be blank, so it should show a blank value in Result.
I created this spreadsheet here to show what I meant above. It can be edited by anyone and doesn't need to login.
Code
=IFERROR(ARRAYFORMULA(
IF(
VLOOKUP(A2:A, Sheet2!A2:D, 2, FALSE) = "OK",
VLOOKUP(A2:A, Sheet2!A2:D, 3, TRUE)
)
))
Reverse the values set
=ARRAYFORMULA(IFERROR(
VLOOKUP(
A:A,
SORT(Sheet2!A:C,ROW(Sheet2!A:A),0),3,0),
))
There is the full explanation
try:
=ARRAYFORMULA(IFNA(VLOOKUP(A:A, SORT(Sheet2!A:C, ROW(Sheet2!A:A), 4, 1), 3, 0)))