Switch true for min and max range in power bi desktop - if-statement

I am trying to specify each dimension range (length, width and height) of values specify criteria based on the length, width and height.
In data table I have 3 columns are length, width,height based on the 3 columns I would like to generate status columns which is based on the below mentioned condition range.
Conditions Range
Length, width and height is start from 1 to 300 then A1.
Length, width and height is start from 301 to 650 then A2.
Length, width and height is start from 651 to 900 then A3.
Length, width and height is start from 901 to 1200 then A4
Length, width and height is start from 1201 above then XXX
Data
LENGTH
WIDTH
HEIGHT
DESIRED RESULT RANGE
NA
NA
NA
NA
20000
5000
230
XX
400
300
140
A1
BLANKS
600
400
285
A2
600
400
285
A2
400
300
150
A1
280
230
170
A1
320
320
320
A1
320
320
320
A1
600
400
140
A1
400
300
140
A1
400
300
140
A1
370
320
340
A1
320
240
250
A1
300
200
90
A1
400
290
140
A1
600
400
285
A1
Table and result look like
Any suggestions please

Well, you may try the following dax measure, as it will assign all row to each category, and assign to Other if does not match any of the criteria, do take note due to your data contain NA, therefore all the value is in Text for that need to convert to number during calculation:
Result = IF(Sheet1[LENGTH] = "NA" || Sheet1[WIDTH] = "NA" || Sheet1[HEIGHT] ="NA", "NA",
IF(VALUE(Sheet1[LENGTH])>0 && VALUE(Sheet1[LENGTH]) <=300 && VALUE(Sheet1[WIDTH]) >0 && VALUE(Sheet1[WIDTH]) <=300 && VALUE(Sheet1[HEIGHT]) >0 && VALUE(Sheet1 [HEIGHT]) <=300, "A1",
IF(VALUE(Sheet1[LENGTH])>300 && VALUE(Sheet1[LENGTH]) <=650 && VALUE(Sheet1[WIDTH]) >300 && VALUE(Sheet1[WIDTH]) <=650 && VALUE(Sheet1[HEIGHT]) >300 && VALUE (Sheet1[HEIGHT]) <=650, "A2",
IF(VALUE(Sheet1[LENGTH])>650 && VALUE(Sheet1[LENGTH]) <=900 && VALUE(Sheet1[WIDTH]) >650 && VALUE(Sheet1[WIDTH]) <=900 && VALUE(Sheet1[HEIGHT]) >650 && VALUE (Sheet1[HEIGHT]) <=900, "A3",
IF(VALUE(Sheet1[LENGTH])>900 && VALUE(Sheet1[LENGTH]) <=1200 && VALUE(Sheet1[WIDTH]) >900 && VALUE(Sheet1[WIDTH]) <=1200 && VALUE(Sheet1[HEIGHT]) >900 && VALUE(Sheet1[HEIGHT]) <=1200, "A4",
IF(VALUE(Sheet1[LENGTH])>1200 && VALUE(Sheet1[WIDTH]) >1200 && VALUE(Sheet1[HEIGHT]) >1200 , "XXX",
"Others"))))))
Result:

Related

DAX expression for sumarizing values to a set date

I have done some tests myself but all has been unsuccessful, but if I am missing something please feel free to ask for more information.
I have a spreadsheet with contract numbers on the rows and amortizations on the columns, like the following example:
Original Value
2011
2012
2013
2014
2105
1001
2000
10
10
10
10
10
1002
1500
20
20
20
20
20
1003
1200
0
0
15
15
15
1004
3000
0
0
0
0
10
Using DAX is there a good way to make a calculated value for the present value for each of the time frames? As the example below show:
Original Value
PV2101
PV2102
PV2103
PV2114
PV2105
1001
2000
1990
1980
1970
1960
1950
1002
1500
1480
1460
1440
1420
1400
1003
1200
0
0
1185
1170
1155
1004
3000
0
0
0
0
2990
So a calculated value that sums upp all prior amortizations dependant on the date in the x-axis and reduces that amount from the original value.
I can see two options:
You create a pivoted table:
|ID |Year|Value|
|----|----|----|
|1001|2011| 10|
|1001|2012| 10|
|1001|2013| 10|
|1001|2014| 10|
|1001|2015| 10|
|1002|2011| 20|
And then your measure will be trivial:
Calculated Value =
VAR dt_year = YEAR(SELECTED(date[date]))
RETURN SUM(table[Original Value]) +
CALCULATE(SUM(amort[Value]), amort[Year] <= dt_year)
Or you create some measure that adds up the individual columns:
Calculated Value =
VAR dt_year = YEAR(SELECTED(date[date]))
RETURN SUM(table[Original Value]) +
IF(dt_year >= 2011, SUM(table[2011]), 0) +
IF(dt_year >= 2012, SUM(table[2012]), 0) +
IF(dt_year >= 2013, SUM(table[2013]), 0) +
IF(dt_year >= 2014, SUM(table[2014]), 0) +
IF(dt_year >= 2015, SUM(table[2015]), 0)
Option 1 is a lot nicer.

INDEX and AGGREGATE FUNCTION in Power BI

I have a two tables are data and report.
In data table the following columns are Size A, Size B and Size C, Type and Rank.
In data table I created rank for each type based on the sizes. The purpose of the rank column were multiple matches for same size in this case the rank column will help to decide exact type were match more than one.
In report table the following columns are Size A, Size B and Size C.
In both table the Size A, Size B and Size C columns are common/relationship.
I am trying find out the appropriate type according to the Size A, Size B and Size C from data table into report table.
Data:
TYPE
SIZEA
SIZEB
SIZEC
RANK
A6
420
600
440
11.00
A4
640
600
480
9.00
A5
890
1100
1330
2.00
A6
1335
1100
2350
1.00
A7
890
1100
390
5.00
A8
890
1100
530
3.00
A9
670
1100
540
4.00
A10
670
1100
440
6.00
A11
320
1100
440
10.00
A12
600
400
400
12.00
A13
800
600
400
8.00
A14
1000
600
500
7.00
Report:
SIZEA
SIZEB
SIZEC
DESIRED RESULT-TYPE
400
300
140
A12
A12
250
250
160
A12
600
400
285
A12
400
300
150
A12
280
230
170
A12
320
320
320
A12
320
320
320
A12
600
400
140
A12
400
300
140
A12
400
300
140
A12
370
320
340
A12
320
240
250
A12
300
200
90
A12
400
290
140
A12
I am applying following formula in report table in order to get the appropriate type according to the Size A, Size B and Size C
=INDEX(DATA!$D$2:$D$16,AGGREGATE(15,6,(ROW(DATA!$H$2:$H$16)-1)/(DATA!$H$2:$H$16=1/(1/MAX(((DATA!$E$2:$E$16>=$B3)*(DATA!$F$2:$F$16>=$A3)+(DATA!$E$2:$E$16>=$A3)*(DATA!$F$2:$F$16>=$B3)>0)*(DATA!$G$2:$G$16>=$C3)*DATA!$H$2:$H$16))),1))
How can I apply the same logic in Power BI? Any advise please.
I am looking for new calculate column options. Herewith share the Excel file for your reference
https://www.dropbox.com/scl/fi/iq0gteeyazrg79q7a4tb1/AUTO-MODIFY-REQ.xlsx?dl=0&rlkey=nyyerjsg7if2dz30z9iqo6kdc
Here is another formula that will return the same result as per your current formula, although it is still a long formula, but more easily to understand:
=INDEX($D$2:$D$13,MATCH(MAX(IF($G$2:$G$13<$L3,0,
IF((IF($E$2:$E$13<$K3,0,1)*IF($F$2:$F$13<$J3,0,1))+(IF($E$2:$E$13<$J3,0,1)*IF($F$2:$F$13<$K3,0,1))>0,1,0))
*$H$2:$H$13),$H$2:$H$13,0),1)
So I will break it down to explain how do it working:
Part 1: This if formula is checking comparison of Size C, if your data is lower than than table, the value for the Type will be zero therefore will not be considered at all because zero times any amount will be zero
(IF($G$2:$G$13<$L3,0
Part 2: This part is will first checking comparison of Size A vs Size B, if lower than table than return 0 and apply for other comparison, at then end using If at the beginning to reset the value so than the final value will be 0 or 1
IF((IF($E$2:$E$13<$K3,0,1)*IF($F$2:$F$13<$J3,0,1))+(IF($E$2:$E$13<$J3,0,1)*IF($F$2:$F$13<$K3,0,1))>0,1,0)
Part 3: From the value you calculate, if 0 times any ranking will be zero, so from none zero ranking such as 12 or 9, it will get the max value from the list, most of them are 12
Max((.....)*$H$2:$H$13)
Part 4: Index Match - Finally it is same as your formula first part, but you are using Index row which make it difficult to understand
INDEX($D$2:$D$13,MATCH(Max(...))
A shorter version of your formula by removing unnecessary portion:
=INDEX($D$2:$D$13,MATCH(MAX(IF($G$2:$G$13<$L4,0,
IF(($E$2:$E$13>=$K4)*($F$2:$F$13>=$J4)+($E$2:$E$13>=$J4)*($F$2:$F$13>=$K4)>0,1,0))*$H$2:$H$13),
$H$2:$H$13,0),1)
You can add a colum to your report table like:
Desired = LOOKUPVALUE(DataR[TYPE],DataR[RANK], MAXX(FILTER(DataR, DataR[SIZEA] >= ReportR[SIZEA] && DataR[SIZEB] >= ReportR[SIZEB] && DataR[SIZEC] >= ReportR[SIZEC]), DataR[RANK]))
It first filters the table to find the row where the sizes are more or equal to the report row, selects the rank and then does a lookup to the type.
Result:

Indicator for Top3 and ranking across ros

What I am trying to do are following: I want to find out if a observation (A) is top 3 across others.
For example,
A B C D E F G H TOP3-A
1 20 30 40 50 60 70 80 90 N
2 80 90 70 80 0 0 0 0 Y
3 70 0 0 80 90 0 0 0 Y
4 60 70 80 90 0 0 0 0 N
I am thinking transpose + rank + transpose + if <4 then Y else N, however it seems too cumbersome and to be honest as a newbie I do not how to code all these steps correctly...
Your method would work, but there's a much simpler way of doing it.
You could use an array, which reads across rows, however I'm using an even easier way of reading across rows.
The OF statement can be used in conjunction with a summary function to calculate values across rather than down. The LARGEST function returns the largest nth value from a range, so you can compare field A to the 3rd largest value in the row.
I've given you the answer to produce Y, N plus an alternative that produces 1, 0 which is even simpler.
data have;
input A B C D E F G H;
datalines;
20 30 40 50 60 70 80 90
80 90 70 80 0 0 0 0
70 0 0 80 90 0 0 0
60 70 80 90 0 0 0 0
;
run;
data want;
set have;
if A >= largest(3, of A--H) then top3_A = 'Y'; /* A--H references all columns between A and H */
else top3_A = 'N';
/* or */
top3_A2 = (A >= largest(3, of A--H)); /* returns 1 for true, 0 for flase */
run;

How to extract values from a "table-like" text file with awk

all
I have two input files like this :
file1 :
#W #S #this line dosen't exit
110 170 Bias
110 200 Bias
110 215 Bias
110 320 Bias
125 170 Bias
125 200 Bias
125 215 Bias
125 320 Bias
135 170 Bias
135 200 Bias
135 215 Bias
135 320 Bias
140 170 Bias
140 200 Bias
140 215 Bias
140 320 Bias
file2 :
FUNCTION BIAS ( W, S )
Bias = 0
IF AND ( W >= 0, W < 120 ) THEN
IF ( S >= 0 ) THEN Bias = -1
IF ( S >= 180 ) THEN Bias = -2
IF ( S >= 190 ) THEN Bias = -3
IF ( S >= 200 ) THEN Bias = -4
IF ( S >= 210 ) THEN Bias = -5
IF ( S >= 220 ) THEN Bias = -6
IF ( S >= 240 ) THEN Bias = -7
ENDIF
IF AND ( W >= 120, W < 130 ) THEN
IF ( S >= 0 ) THEN Bias = -11
IF ( S >= 180 ) THEN Bias = -12
IF ( S >= 190 ) THEN Bias = -13
IF ( S >= 200 ) THEN Bias = -14
IF ( S >= 210 ) THEN Bias = -15
IF ( S >= 220 ) THEN Bias = -16
IF ( S >= 240 ) THEN Bias = -17
ENDIF
IF AND ( W >= 130, W < 140 ) THEN
IF ( S >= 0 ) THEN Bias = 1
IF ( S >= 180 ) THEN Bias = 2
IF ( S >= 190 ) THEN Bias = 3
IF ( S >= 200 ) THEN Bias = 4
IF ( S >= 210 ) THEN Bias = 5
IF ( S >= 220 ) THEN Bias = 6
IF ( S >= 240 ) THEN Bias = 7
ENDIF
IF ( W >= 140 ) THEN
IF ( S >= 0 ) THEN Bias = 11
IF ( S >= 180 ) THEN Bias = 12
IF ( S >= 190 ) THEN Bias = 13
IF ( S >= 200 ) THEN Bias = 14
IF ( S >= 210 ) THEN Bias = 15
IF ( S >= 220 ) THEN Bias = 16
IF ( S >= 240 ) THEN Bias = 17
ENDIF
RETURN (Bias)
What I wanna do is to find out the corresponding value of a math function : "BIAS(W,S)" with the input (W,S) pair from file1
for example : W/S=135/195, "W" satisfy
IF AND ( W >= 130, W < 140 )
so we will go to check "S"
IF ( S >= 0 ) THEN Bias = 1
IF ( S >= 180 ) THEN Bias = 2
IF ( S >= 190 ) THEN Bias = 3
IF ( S >= 200 ) THEN Bias = 4
IF ( S >= 210 ) THEN Bias = 5
IF ( S >= 220 ) THEN Bias = 6
IF ( S >= 240 ) THEN Bias = 7
then finally we can find out S=195 is in between 190 and 200, the value of BIAS(W,S) is 3
what I want for the output is like this :
110 170 Bias -1
110 200 Bias -4
110 215 Bias -5
110 320 Bias -7
125 170 Bias -11
125 200 Bias -14
125 215 Bias -15
125 320 Bias -17
135 170 Bias 1
135 200 Bias 4
135 215 Bias 5
135 320 Bias 7
140 170 Bias 11
140 200 Bias 14
140 215 Bias 15
140 320 Bias 17
It's very easy to check by human eyes
but as you can see, file2 is basically a text file instead of a regular 2D-array numerical file, How can I extract the corresponding value? Any hint?
I just translated your logic into awk:
script.awk:
{
w=$1;
s=$2;
if (w >= 0 && w < 120) {
if ( s >= 0) { bias= -1 }
if ( s >= 180 ) { bias= -2 }
if ( s >= 190 ) { bias= -3 }
if ( s >= 200 ) { bias= -4 }
if ( s >= 210 ) { bias= -5 }
if ( s >= 220 ) { bias= -6 }
if ( s >= 240 ) { bias= -7 }
}
if (w >= 120 && w < 130) {
if ( s >= 0) { bias= -11 }
if ( s >= 180 ) { bias= -12 }
if ( s >= 190 ) { bias= -13 }
if ( s >= 200 ) { bias= -14 }
if ( s >= 210 ) { bias= -15 }
if ( s >= 220 ) { bias= -16 }
if ( s >= 240 ) { bias= -17 }
}
if (w >= 130 && w < 140) {
if ( s >= 0) { bias= 1 }
if ( s >= 180 ) { bias= 2 }
if ( s >= 190 ) { bias= 3 }
if ( s >= 200 ) { bias= 4 }
if ( s >= 210 ) { bias= 5 }
if ( s >= 220 ) { bias= 6 }
if ( s >= 240 ) { bias= 7 }
}
if (w >= 140 ) {
if ( s >= 0) { bias= 11 }
if ( s >= 180 ) { bias= 12 }
if ( s >= 190 ) { bias= 13 }
if ( s >= 200 ) { bias= 14 }
if ( s >= 210 ) { bias= 15 }
if ( s >= 220 ) { bias= 16 }
if ( s >= 240 ) { bias= 17 }
}
print $0" "bias;
}
Execution:
awk -f script.awk file1
110 170 Bias -1
110 200 Bias -4
110 215 Bias -5
110 320 Bias -7
125 170 Bias -11
125 200 Bias -14
125 215 Bias -15
125 320 Bias -17
135 170 Bias 1
135 200 Bias 4
135 215 Bias 5
135 320 Bias 7
140 170 Bias 11
140 200 Bias 14
140 215 Bias 15
140 320 Bias 17
Run the tst.awk script below on "file2" to convert the script in whatever language that is to awk and save it's output to a new file named "getbias.awk", then run:
awk -f getbias.awk '<your script>' file1
where <your script> parses file1 and calls the generated getbias() function below to get the bias values for each line.
$ cat tst.awk
{
sub(/BIAS/,"getbias")
sub(/ENDIF/,"}")
sub(/ THEN/,"")
$0 = tolower($0)
}
/^function/ { sub(/\)/,",\tbias )"); $0 = $0 " {" }
/^return/ { $0 = $0 ORS "}" }
/^if/ { sub(/ and/,""); sub(/,/," \\&\\&"); $0 = $0 " {" }
{ print }
.
$ awk -f tst.awk file2
function getbias ( w, s , bias ) {
bias = 0
if ( w >= 0 && w < 120 ) {
if ( s >= 0 ) bias = -1
if ( s >= 180 ) bias = -2
if ( s >= 190 ) bias = -3
if ( s >= 200 ) bias = -4
if ( s >= 210 ) bias = -5
if ( s >= 220 ) bias = -6
if ( s >= 240 ) bias = -7
}
if ( w >= 120 && w < 130 ) {
if ( s >= 0 ) bias = -11
if ( s >= 180 ) bias = -12
if ( s >= 190 ) bias = -13
if ( s >= 200 ) bias = -14
if ( s >= 210 ) bias = -15
if ( s >= 220 ) bias = -16
if ( s >= 240 ) bias = -17
}
if ( w >= 130 && w < 140 ) {
if ( s >= 0 ) bias = 1
if ( s >= 180 ) bias = 2
if ( s >= 190 ) bias = 3
if ( s >= 200 ) bias = 4
if ( s >= 210 ) bias = 5
if ( s >= 220 ) bias = 6
if ( s >= 240 ) bias = 7
}
if ( w >= 140 ) {
if ( s >= 0 ) bias = 11
if ( s >= 180 ) bias = 12
if ( s >= 190 ) bias = 13
if ( s >= 200 ) bias = 14
if ( s >= 210 ) bias = 15
if ( s >= 220 ) bias = 16
if ( s >= 240 ) bias = 17
}
return (bias)
}

Bash: expand a list of coordinates (sed?)

I have a list of simple coordinates (longitude, latitude pairs) like
110 30
-120 0
130 -30
0 30
and try to expand it to this:
110 30 110\272E 30\272N 110 30 LON0
-120 0 120\272W 0\272 -120 0 LON0
130 -30 130\272E 30\272S 130 -30 LON0
0 30 0\272 30\272N 0 30 LON0
Examining the first line:
110 30 110\272E 30\272N 110 30 LON0
110 30 The first two values just stay the same
110\272E the third value is basically the first value with an added (octal \272) degree symbol and an E for positive values or a W for negative values
30\272N similar to the third value, this is the latitude with an added degree symbol and a N for positive and a S for negative values.
110 30 is just a repetition of the first two values
LON0 is a fixed string for later replacement.
Things tried so far:
I played around with sed, but was unable to achieve anything remotely useful. I wasn't able to manipulate the matched values depending on them being negative or positive.
Any help is greatly appreciated.
All the best,
Chris
EDIT: #jaypal suggested to add different possible cases that can occur. Original was only one case with minor deviations in value.
EDIT2: Had to adjust the example data due to me not updating all values in the sample data. My apologies.
Can you use awk? It will be very easy:
$ cat file
110 30
-120 0
130 -30
0 30
awk '
function abs(x) {
x = x > 0 ? x : x * -1
return x
}
{
print abs($1),abs($2), ($1>0?abs($1)"\272E":$1==0?$1"\272":abs($1)"\272W"), ($2>0?abs($2)"\272N":$2==0?$2"\272":abs($2)"\272S"), abs($1), abs($2), "LON0"
}' file
110 30 110ºE 30ºN 110 30 LON0
120 0 120ºW 0º 120 0 LON0
130 30 130ºE 30ºS 130 30 LON0
0 30 0º 30ºN 0 30 LON0
If you want to print \272 instead of º just add another backslash to prevent it from interpolating. So modify the above script and use \\272 where ever you see \272.
We print the fields as you desire in your output and the following two syntax:
($1>0?$1"\272E":$1"\272W")
($2>0?$2"\272N":$2"\272S")
are ternary operators that checks for the positivity of the values. If first is positive use E else W. If second is positive use N else use S.
Update:
awk '
function abs(x) {
x = x > 0 ? x : x * -1
return x
}
{
print $1,$2,($1>0?$1"\\272E":$1==0?$1"\\272":abs($1)"\\272W"),($2>0?$2"\\272N":$2==0?$2"\\272":abs($2)"\\272S"),$1,$2, "LON0"
}' file
110 30 110\272E 30\272N 110 30 LON0
-120 0 120\272W 0\272 -120 0 LON0
130 -30 130\272E 30\272S 130 -30 LON0
0 30 0\272 30\272N 0 30 LON0