PowerBI - concatenate column values ​in measure - powerbi

i need concatenate column values (B) ​​in measure
Table1:
A B
1 RED
2 GREEN
3 BlUE
4 RED
5 BLACK
in measure = RED GREEN BLUE RED BLACK
How can i do this?

You can use CONCATENATEX() function.
Here is a simple example:
B values =
CONCATENATEX(
VALUES('Table'[B]),
'Table'[B],
" "
)
Result:

Related

How can I get the last value of a column in my dataset in power bi?

In the given table how to get the last value of a particular column in a dataset
Roll No Name Index Score
1 ab1 1 23
2 ab2 2 43
3 ab3 3 42
Here we have to pick the Last Row Score value?
1) The first thing we have to create a custom index column in the table
2) Then we have to use the below formula to get the last Row Score value Score value
CALCULATE(LASTNONBLANK(Table[Score], 1), FILTER(ALL(Table), Table[Index] = MAX(Table[Index])))
The result would be 42.

How to populate a cell with information stored in a location relative to the value in another cell?

Let's say I have
Sheet 1
A B C
1 # 20 30
2 # 75 90
3 # 46 21
Sheet 2
A B C
1 X Y
Where "X" is a dropdown list of Sheet 1's B column.
"Y" is where I want to fill in the value in the C column of the respective row of Sheet 1 for Sheet 2's column A selection.
How might I do this?
try:
=VLOOKUP(A1; 'Sheet 1'!B:C; 2; 0)
for arrayformula use:
=ARRAYFORMULA(IFNA(VLOOKUP(A1; 'Sheet 1'!B:C; 2; 0)))
In C1 try
=IF(LEN(A1), VLOOKUP(A1, Sheet1!B:C, 2, 0),)

power bi: split column values by character and calculate sum

I have a table in which numeric values are there, but in some rows may contains values separated by comma. Since comma is in there so the data type of the column will be text.
I want to calculate sum of this column.
Here is my table-
Id Values
A 12
B 13
C 15
D 13,11,12
E 16
I want my sum to be - 12+13+15+13+11+12+16
Since the datatype of this column will be text so can the sum be calculated like I want or I have to do something like this-
Id Values
A 12
B 13
C 15
D 13
D 11
D 12
E 16
You can split the column by delimiter:
And split it into rows instead of columns:
The result will be as what you expected:
Then you can sum it as usual with DAX:
Sum = SUM(Data[Values])

Carrying string labels of string variable after reshape

I have dataset in Stata that looks like this
entityID indicator indicatordescr indicatorvalue
1 gdp Gross Domestic 100
1 pop Population 15
1 area Area 50
2 gdp Gross Domestic 200
2 pop Population 10
2 area Area 300
and there is a one-to-one mapping between values of indicator and values of indicatordescr.
I want to reshape it to wide, i.e. to:
entityID gdp pop area
1 100 15 50
2 200 10 300
where I would like gdp variable label to be "Gross Domestic", pop label "Population" and area "Area".
Unfortunately, as I understand, it is not possible to assign the value of indicatordescr as a value label of indicator, so the reshape can't transform these value labels into variable labels.
I have looked at this : Bring value labels to variable labels when reshaping wide
and this : http://www.stata.com/support/faqs/data-management/apply-labels-after-reshape/
but did not understand how to apply those to my case.
NB: the variable labeling after reshape must be done programatically, because indicator and indicatordescr have many values.
"String labels" here is informal; Stata does not support value labels for string variables. However, what is wanted here is that the distinct values of a string variable become variable labels on reshaping.
Various work-arounds exist. Here's one: put the information in the variable name and then take it out again.
clear
input entityID str4 indicator str14 indicatordescr indicatorvalue
1 gdp "Gross Domestic" 100
1 pop "Population" 15
1 area "Area" 50
2 gdp "Gross Domestic" 200
2 pop "Population" 10
2 area "Area" 300
end
gen what = indicator + "_" + subinstr(indicatordescr, " ", "_", .)
keep entityID what indicatorvalue
reshape wide indicatorvalue , i(entityID) j(what) string
foreach v of var indicator* {
local V : subinstr local v "_" " ", all
local new : word 1 of `V'
rename `v' `new'
local V = substr("`V'", strpos("`V'", " ") + 1, .)
label var `new' "`V'"
}
renpfix indicatorvalue
EDIT If the length of variable names bites, try another work-around:
clear
input entityID str4 indicator str14 indicatordescr indicatorvalue
1 gdp "Gross Domestic" 100
1 pop "Population" 15
1 area "Area" 50
2 gdp "Gross Domestic" 200
2 pop "Population" 10
2 area "Area" 300
end
mata : sdata = uniqrows(st_sdata(., "indicator indicatordescr"))
keep entityID indicator indicatorvalue
reshape wide indicatorvalue , i(entityID) j(indicator) string
renpfix indicatorvalue
mata : for(i = 1; i <= rows(sdata); i++) stata("label var " + sdata[i, 1] + " " + char(34) + sdata[i,2] + char(34))
end
LATER EDIT Although the above is called a work-around, it is a much better solution than the previous.

Equivalent of Access Crosstab Query in SAS?

Here is my Input:
ID Color
1 green
1 red
1 orange
1 green
1 red
2 red
2 red
2 blue
3 green
3 red
Here is what I want in my output - a count of records by ID for each color:
ID green red orange blue
1 2 2 1 0
2 0 2 0 1
3 1 1 0 0
I know I can get the information using proc freq, but I want to output a dataset exactly like the one I have written above. I can't seem to figure out how to make the colors the columns in this output dataset.
first, generate the data.
data data;
format ID 8. Color $8.;
input id color;
datalines;
1 green
1 red
1 orange
1 green
1 red
2 red
2 red
2 blue
3 green
3 red
run;
next, summarize color counts by id.
proc freq data=data noprint;
table id*color / out=freq;
run;
make the table flat.
proc transpose data=freq out=freq_trans(drop=_:);
id color;
by id;
var count;
run;
optionally, fill in missing cells with 0.
data freq_trans_filled;
set freq_trans;
array c(*) green red orange blue;
do i = 1 to dim(c);
if c[i]=. then c[i]=0;
end;
drop i;
run;
You can fill the missing cells with zero's using the SPARSE option to the PROC FREQ's TABLE statement. This way, you don't need another DATA step. The order of the colors can also be controlled by the ORDER= option to PROC FREQ.
data one;
input id color :$8.;
datalines;
1 green
1 red
1 orange
1 green
1 red
2 red
2 red
2 blue
3 green
3 red
run;
proc freq data=one noprint order=data;
table id*color /out=freq sparse;
run;
proc transpose data=freq out=two(drop=_:);
id color;
by id;
var count;
run;
proc print data=two noobs;
run;
/* on lst
id green red orange blue
1 2 2 1 0
2 0 2 0 1
3 1 1 0 0
*/
I've never been a fan of proc transpose because I can never remember the syntax. Here's a way to do it with proc sql and a macro variable.
proc sql noprint;
select sum(color = '" || trim(color) || "') as " || color into: color_list separated by ", "
from (select distinct color from one);
create table result as
select id,
&color_list
from one
group by id;
quit;
id blue green orange red
1 0 2 1 2
2 1 0 0 2
3 0 1 0 1
For (pteranodon), I happened to be reviewing the archives(6+ yrs later) which is why so untimely, but someone may benefit.
proc sql noprint feedback;
select catx(' ','sum(color =',quote(trim(color)),') as',color) into: color_list separated by ", "
from (select distinct color from one);
create table result as
select id, &color_list
from one
group by id;
quit;