How to select N columns from a PowerBI table? - excel

I have the following table with three columns A, B, and C on PowerBI, from which I want to select columns A and B to create a new table.
And looking from the equivalent in Pandas to:
table_2 = table1[["A","B"]]
or from SQL:
SELECT A,B FROM table1;
But I'm not finding the equivalent function on Power BI; the SELECTCOLUMNS function is used to create a new column as stated in the docs:
Adds calculated columns to the given table or table expression.

The SELECTCOLUMNS function works fine for this. It allows you to create more complex calculated columns, but you can simply use the column itself as the calculation definition.
This should do the trick:
SELECTCOLUMNS(table1, "A", table1[A], "B", table1[B])

Related

Excel formula in Power Query

I would like to use the following excel (table) function in Power Query:
{=IF([#[Month_N]]=MIN(IF([Pers_ID]=[#[Pers_ID]];[Month_N]));TRUE;FALSE)}
It gives me for a distinct Pers_ID with the lowest Month_N number a TRUE and otherwise a FALSE.
Is there a way to do this in Power Query?
Reason: I would like to automate different steps in Excel via power query so I can start analyzing my data right away.
Assuming your table is added to Power Query as Table1:
Duplicate the Table1 query, renaming this new query as Table2
Add a step to the Table2 query in which you use the Group By feature, grouping on Pers_ID, with New column name=MinMonth, Operation=Minand Column=Month_N
Back in the Table1 query, Merge Queries (not as New) on Table 1 and Table2, choosing the Pers_ID column from both, Left Outer join.
Expand the Table2 column after this merge, selecting the MinMonth column only
Add a simple Calculated Column to check if the entry in the MinMonth column is equal to that in the Month_N column
Delete unwanted columns as required

In power bi how to convert number to string

In my data table i have column category types (1, 2 , 3). I want to change them to say 1 = hot desk, 2 = fix desk, 3 = private company. how to do it? I'm oky to create new column if needed.
There are several ways of doing this.
One way is to create a second table, a lookup table, with two columns [ID] and [Text]. The ID column has a single row for each of your category types (1, 2, 3) and the second column contains the corresponding string value. Then you create a 1:* relationship between category type column and ID column. In a visual you can then build the visual with the information from the table containing the category type column and the string value from the new lookup table.
A second way is creating a calculated column in the original table using an IF or SWITCH statement.
Which solution is the best for you depends on how many different category types and how many rows of data you have in the data table. intuitively I would say that the lookup table option is better from a resource usage.
Cheers,
Oscar
Oscars solution should work. You could also create a Conditional Column in Power Query.
Add Column Tab > General Group > Conditional Column.

Excel Formula to DAX: How to Reference Previous Row

I'm trying to create a new column in Power BI Desktop which replicates a basic Excel formula which seems simple enough.
The Excel formula which I am trying to replicate is in column D:
=IF(B2=B1, 0, C2)
So that the returned column should look like column D.
Excel Data Example:
I have the same data structure in Power BI and would like to create a new column to replicate column D in Excel, but I can't find a way to replicate the formula in DAX where it calls for :
B2=B1
Power BI Data Example:
Thanks!
The issue here is that DAX doesn't do relative references between the rows in the same way since there is no inherent ordering of the table. You'll have to use the index column in order to find the appropriate row.
In this case, what you can do it look up the [Cage No] in the row where the [Index Order] is one less than in the current row:
Reported Size = IF(
LOOKUPVALUE(
Table1[Cage No],
Table1[Index Order], Table1[Index Order] - 1
) = Table1[Cage No],
0,
Table1[Size] )

Excel PivotTable Count field as % of another Count field

I am using Excel from Microsoft Office 365 ProPlus.
Here's a really simple data table.
I want to build a Pivot Table around it to look like this...
... except that the "what I want" column (which is the count of items in Column C divided by the count of items in Column B) should be a part of the pivot table.
I have tried all sorts of things using calculated fields, calculations on fields, etc., to add the "what I want" column and just cannot make it work.
Can anyone help?
Calculated Fields only operate on the Sum of the elements in the data tables. Wherever you see a Field Name in the formula for a Calculated Field, picture it as meaning the sum of all elements for that field (that match any other row/column criteria in the Pivot Table).
Putting "= B / C" actually means "= SUM(B) / SUM(C)" for elements of columns B and C that fit that section of the Pivot Table.
The only way to achieve your goal is with two helper columns:
B Count: =COUNT([#B])
C Count: =COUNT([#C])
The sum of these columns then give you the count of columns B and C, so you can use these helper columns to give you what you want:
The Data Field based on the Calculated Field then says "Sum of What U Want", but it will always just be the result of your calculation, even if you change how the field is summarized through Value Field Settings. You can manually rename the Data Field, but it still needs to be different to the Calculated Field name you chose earlier.
Click in the Pivot Table, then go to the Analyze tab, click on Fields, Items, & Sets, then select Calculated Field. Your formula is probably
= B / C

VLOOKUP with criterion in range

I have a table A that contains ID (i.e. SR1000). Also a table B with two rows, another ID (i.e. C-1) and text on the second row.
I was trying to use VLOOKUP using the ID on table A to return ID on Table B if ID from Table A is somewhere in the text of column 2 in table B.
Table A
SR1000
SR1001
Table B
C-1|dummy text
C-2|dummy SR1000 dummy
So it should return C-2 when looking for SR1000.
Is VBA my only option? Trying to avoid reinventing the wheel.
It seems the formula:
=INDEX(F:F,MATCH("*"&B4&"*",G:G,0))
has been deemed fit for purpose and by way of explanation this was based on assumed locations as below:

Resources