Finding duplicate row and flagging first row as 1 and remaining duplicate rows 0 in power query - duplicates

I want solutions for power query.
I have one column of document numbers but it's comes as per materials sold so same document number rows increased, I need to define first row as 1 and remaining rows as 0 in Excel power query.
If anyone have solution then please share to me like m function/formula

Right click the Documentnumber column, choose Group By, use all the default selections
Adjust the formula in the formula bar from
= Table.Group(Source, {"Documentnumber"}, {{"Count", each Table.RowCount(_), type number}})
to
= Table.Group(Source, {"Documentnumber"}, {{"Count", each Table.AddIndexColumn(_, "Index", 1, 1), type table}})
Expand the index column using the arrows atop the new column
Add column .. custom column ... with formula
= If Index = 1 then 1 else 0
Remove index column
File .. close and load...
Sample code
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Documentnumber"}, {{"Count", each Table.AddIndexColumn(_, "Index", 1, 1), type table}}),
#"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Index"}, {"Index"}),
#"Added Custom" = Table.AddColumn(#"Expanded Count", "Custom", each if [Index]=1 then 1 else 0),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in #"Removed Columns"

Related

Power BI - How to combine all the values in a column from another query (table) in Power Query

I'm trying to write a power query to where I can join all the values of a column from another query (table) into a single line.
Example:
Table from Query 1
Outcome: I want it to return a text like:
[2019-09],[2019-10],[2019-11],[2019-12],[2020-01],[2020-02]
I'm trying to put this in my other query, within a JSON code, where the text combine function is (Value).
{""DataModelName"":""[AllStreams].[Month Year]"",
""Caption"":"""&Date.ToText( DateTime.Date( Date.AddDays(DateTimeZone.UtcNow(),0) ) , "yyyy-MM")&""",
""Value"":""["
& Text.Combine(Table.SelectColumns(FilterList_PV)[Date]), "" ) & "]"",
""Operand"":0,
""UnionGroup"":""""}
Let me know if this is possible! Thanks!
Here's another way: Text.Combine(List.Transform(Table[Column], Text.From),",")
you can do this with one big step, but shown here in smaller pieces
Assuming column in question is named Date
Add a column we can use to group on, all with same value
Add column that converts each date into text surrounded by [] showing year and month
Group on the first added column, combining all row values
Remove extra column
#"Added Custom" = Table.AddColumn(#"PriorStep", "Custom", each "[" & Text.From(Date.Year([Date])) & "-" & Text.PadStart(Text.From(Date.Month([Date])),2,"0") &"]"),
#"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 0),
#"Grouped Rows" = Table.Group(#"Added Index", {"Index"}, {{"Count", each Text.Combine( List.Transform([Custom], Text.From), ","), type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Index"})

Sql moving values from one column to two different columns

My problem is as follows - in the database I have a Products table with a size column. However, I have now created two new columns x_size and y_size and I wanted to move the values from the size column to these 2 columns. For example, in the database I have a record, where the size column value is 100x200, now I want to transfer '100' to the new x_size column and '200' to the y_size column, and so for each of the records in this table.
I was trying with :
UPDATE `post` SET `x_size `=`size`
UPDATE `post` SET `y_size `=`size`
But this updates the value of the entire size column, not just part of it. I will be grateful for your help
If you have multiple records that you want to transfer from size column, you could do fetch all the records from your table, and then split your size values using PHP explode() function. Since you tagged Laravel, here's the simple Laravel solution:
$products = Products::get();
foreach ($products as $product){
$size = explode('x', $product->size);
$product->x_size = $size[0]; //First element from exploded array
$product->y_size = $size[1]; //Second element from exploded array
$product->save();
}
Tinker output:
>>> $size = '100x200';
=> "100x200"
>>> $exlpodedArray = explode('x', $size);
=> [
"100",
"200",
]
>>> $x_size = $exlpodedArray[0];
=> "100"
>>> $y_size = $exlpodedArray[1];
=> "200"
You can use substring_index():
UPDATE post
SET x_size = substring_index(size, 'x', 1),
y_size = substring_index(size, 'x', -1)

PowerBI multiple if conditions for a calculated column

Im working towards creating a calculated column that should contain an integer value specified by me if the criteria is met. So, from a single table, I want to check values of few columns, if the values that i specify are present in those column then the custom column should return an integer value specified by me. For example
Column A, Column B, Column C
arizona, 3, 3109
colorado,4, 2353
.
.
.
california,23, 6978
I want to create a custom column in such a way that
if column a='california' && column b='3' && column c= '3109' then 7 elseif
column a='california' && column b='5' && column c='3109' then 8 elseif and so on.
I have tried all the possible functions in PowerBi but it is not giving the desired output.
any kind of lead will be appreciated.
Thanks
Best way to do this is in the query editor. Go to Edit Queries > Add Column > Custom Column and use following code:
= if [Column A] = "california" and [Column B] = "3" and [Column C] = "3109" then 7
else if [Column A] = "california" and [Column B] = "5" and [Column C] = "3109" then 8
else if ...more conditions...
else 0 ' <- This one is for the case that no if condition is met and closes the if
Note that if Column B and Column C of type numberic you have to write it without the quotes. Like this:
= if [Column A] = "california" and [Column B] = 3 and [Column C] = 3109 ...

calculate the sum of a column based on another column values(SSRS)

The data is as shown below(Source description and total shares) are the two columns
Source 494,482,729,319.98998
Initial
Filtered
Final 494,482,729,319.98998
My expression throws an #Error in output of the report(preview section)
=SUM( Fields!total_shares.Value *
SWITCH(Fields!Source_Description.Value = "Initial" OR Fields!Source_Description.Value = "Inserted", 1,
Fields!Source_Description.Value = "Filtered" OR Fields!Source_Description.Value = "Deleted" OR Fields!Source_Description.Value = "Final", -1,
True,0
)
)
Can you please help me where am I going wrong?

create a set expression for a cell in a table report

I have a report which creates a spreadsheet. one cell could have data from one of three fields depending on a fourth field status or field two being blank..
I was thinking of writing as CASE statement but that does not work in ssrs.
The IIF statement works great for 2 values but how do you write for three values?
In common language it would be "If secondary value ="Yes", use secondary name, If account field value is Null, use Contact field value, otherwise use account field value"
Thanks
You can use nested IIF statements but they quickly get messy. You should use SWITCH which acts a bit like a case statement.
A simple example would be
= SWITCH(
Fields!myFirstField.Value = <10 , "Small",
Fields!myFirstField.Value = <30 , "Medium",
Fields!myFirstField.Value = <80 , "Large",
True , "HUGE",
)
As switch stops at the first true expression there is not need to check ranges of values if you get them in the correct order.
The final "True" expression just acts like an ELSE
If you need to check multiple criteria per condition then you can do that too like this..
= SWITCH(
Fields!myFirstField.Value = <10 AND Fields!mySecondField.Value = 1 , "Small Cat",
Fields!myFirstField.Value = <10 AND Fields!mySecondField.Value = 2 , "Small Dog",
Fields!myFirstField.Value = <30 , "Medium Animal",
Fields!myFirstField.Value = <80 , "Large Animal",
True , "HUGE Animal",
)