in PowerBI - How to group a table by a column, and return all unique values (coma seperated) from another column as grouped - powerbi

I am trying to group a table by a column, so the resulted table have unique values in that column, and also returns all the unique values from another column that belonged to the grouped column:
Source:
Country = USA
Cities =
New York
Boston
Chicago
Houston
Transform: group by [Country] column, and return unqiue values from [Cities] and coma seperated:
Country = USA
Cities = New York,Boston,Chicago,Houston
thanks a lot

You can simply use CONCATENATEX in a measure
Measure = CONCATENATEX(VALUES('Table'[Cities]),'Table'[Cities],",")

Related

How to filter a table for distinct values Powerapps

I am new to Powerapps and I have noticed that the Distinct function returns a table of the distinct values(only returns the distinct column not the full row). Is there a way to filter a table so that it returns back a subset of the full table with distinct values in a specified column.
You can use the GroupBy function for this. Take a look at the documentation, or in the example below:
Assuming that cities is a table with the following values:
City
Country
Population
London
UK
8615000
Berlin
Germany
3562000
Madrid
Spain
3165000
Rome
Italy
2874000
Paris
France
2273000
Hamburg
Germany
1760000
Barcelona
Spain
1602000
Munich
Germany
1494000
Milan
Italy
1344000
The expression GroupBy(cities, "Country", "Cities") will return a table with a column "Country", and a column called "Cities" whose value will be a table with all cities for that country.
You can then use functions such as AddColumns and Sum to aggregate the values of the inner table, like in the example below:
AddColumns(
GroupBy(cities, "Country", "Cities"),
"Sum of City Populations",
Sum(Cities, Population))
In your tweets example, if you want to get one tweet from each day, you can have an expression like the one below:
AddColumns(
GroupBy(Tweets, "crf1d_date_index", "Dates"),
"SampleTweet",
First(Dates))
Where it would have a new column with the first tweet from each date. Or if you want a single field from the group, you can have something like this:
AddColumns(
GroupBy(Tweets, "crf1d_date_index", "Dates"),
"FirstTweetTime",
First(Dates).tweet_time)

Check if value is in another table and add columns in Power BI

I have 2 tables, table1 contains some survey data and table2 is a full list of students involved. I want to check if Name in table2 is also found in table1. If yes, add Age and Level information in table2, otherwise, fill these columns with no data.
table1:
id Name Age Level
32 Anne 13 Secondary school
35 Jimmy 5 Primary school
38 Becky 10 Primary school
40 Anne 13 Secondary school
table2:
id Name
1 Anne
2 Jimmy
3 Becky
4 Jack
Expected output:
id Name Age Level
1 Anne 13 Secondary school
2 Jimmy 5 Primary school
3 Becky 10 Primary school
4 Jack no data no data
Update:
I created a relationship between table1 and table2 using the common column id(which can be repeated in table1).
Then I used:
Column = RELATED(table1[AGE])
but it caught error:
The column 'table1[AGE]' either doesn't exist or doesn't have a relationship to any table available in the current context.
There are various ways to achieve the desired output, but the simplest of them I found is to use the RELATED DAX function. If you found this answer then mark it as the answer.
Create a relationship between table1 and table2 using 'Name` column.
Create a calculated column in table2 as:
Column = RELATED(table1[AGE])
Repeat the same step for the Level column also.
Column 2 = RELATED(table1[LEVEL])
This will give you a table with ID, Name, Age, and Level for the common names between the two tables.
Now to fill those empty rows as no data, simply create another calculated column with following DAX:
Column 3 = IF(ISBLANK(table2[Column]), "no data", table2[Column])
Column 4 = IF(ISBLANK(table2[Column 2]), "no data", table2[Column 2])
This will give you the desired output.
EDIT:- You can also use the following formula to do the same thing in a single column
Column =
VAR X = RELATED(table`[AGE])
VAR RES = IF(ISBLANK(X), "no data", X)
RETURN
RES
and
Column 2 =
VAR X = RELATED(table1[LEVEL])
VAR RES = IF(ISBLANK(X), "no data", X)
RETURN
RES
This will also give you the same output.

Count of column values in Table 1 present in Table 2 column - Power BI

I have two tables
table 1
product_name
ID.
abc
123
abc
456
table 2
product_name
ID.
abc
123
report layout
I want to know how many of them downloaded the trial product and out of those how many purchased. Left side trials, right sight purchases
PS: columns are not unique
Your example is rather vague. I am not sure what values you have in both tables.
You want for the "left visual" count unique IDs, right?
LeftVisual = calculate( DISTINCTCOUNT(table1[id])
And for the right side you want to count ID from Table2 only when that IDs appear in Table1?
RightVisual = calculate ( DISTINCTCOUNT(table2[id], TREATAS(table2[id],table1[id]) ))
Count1 = calculate(DISTINCTCOUNT(table1[id])
Count2 = Calcluate(DISTINCTCOUNT(table2[id]),
INTERSECT(SELECTCOLUMNS(TABLE2, "id", [ID]), SELECTCOLUMNS(TABLE1, "id", [ID])))

Sum based on two columns from another table

I've got this table named "A" and I want to fill the Sales SUM column with the sum of sales from another table group by date and country. The other table is named "B" and got also Date, Country and Sales columns however the number of dates and countries differ. I don't want to join this tables I would like to achieve this in DAX. Is it possible?
Yes you can do that with DAX and virtual relationship:
SumSales = calculate( sum('B'[Sales])
, TREATAS(SUMMARIZE('A','A'[Date],'A'[Country]), 'B'[Date],'B'[Country])
)

Pivot DataFrame

I have a data file in this format:
I want the columns to be grouped by month in a pivot table. When I pivot the data a column for each day is being created.
df = ex.read_excel("C:\\ExportReport.xlsx", "ExportReport")
table = pd.pivot_table(df, values='Forecast Qty', rows='Part', cols='Due Date', aggfunc=np.sum, fill_value=0)
Is there a way to tell pandas to group the columns by month?
Need to have a field that calculates the month. If this is going to span multiple years, will need to combine into one field.
df['YYYY-MM'] = df['Due Date'].apply(lambda x: x.strftime("%Y-%m"))
Then try yours, but change to the monthly field...
table = pd.pivot_table(df, values='Forecast Qty', rows='Part', cols='YYYY-MM', aggfunc=np.sum, fill_value=0)