Tableau : display aggregate string rows for each category - filter

I have data as below
For each category, I want to display the last row in the sub category only, ie last() == 0 should be displayed for sub category
Output as below
Something similar to LOD but only dimensions here

It seems to me that for each ID, you have to check the Last column and, in that "partition", take the related value of subcategory if the value is 0.
{ FIXED [ID] : MAX(if [Last] = 0 then [Subcategory] end)}

Related

I need to perform subtraction and a filter in the same column that I create in Power BI

enter image description here
I need to create a column which subtracts [Retailer_yes_amount] and [classification_base_amount] and at the same time filter out "Not Eligible" category in [Classification] column. [Classification] column has 5 categories - Platinum, Gold, Silver, Bronze and Not eligible.
I was thinking like this New_column = calculate(([Retailer_yes_amount]-[classification_base_amount]),filter('table_name',[classification] <> "Not Eligible")) but it threw an error.
Kindly suggest
If you want to have this evaluated for every row as a new column you have to enter the following expression as a Calculated Column
New_column =
IF(
table_name[Classification] <> "Not Eligible",
[Retailer_yes_amount] - [classification_base_amount]
)
If you want to use a measure you have to specify an aggregation.

Power Query - Fill Down with special text conditions

I am trying to clean a large detailed profit and loss report and it includes Category & Sub Category columns. I have successfully filled down the Category column. However, the Sub Category column only has subcategory names scattered throughout the report therefore a normal fill down won't work.
How do I Fill Down starting where there exists a Sub Category value but only continues down to the Sub Category Total description?
Example in the picture below: Sub Category = "Closing stock - cattle"
Fill Down with those exact words - Closing stock - cattle until the cell that reads Total Closing stock - cattle. Then Fill Down from the new Sub Category in the same format.
Basically, the word Total is a very important part as I do not want that particular value Filled Down. Please note, there can be hundreds of rows that do not have a Sub category.
Filter out the total rows into their own table, fill down, then append the total rows back in.
let
StartTable = <Your Data Source>,
NoTotals = Table.SelectRows(StartTable, each not Text.Contains([Sub Category], "Total") or [Sub Category] = null),
OnlyTotals = Table.SelectRows(StartTable, each Text.Contains([Sub Category], "Total")),
FillDown = Table.FillDown(NoTotals, {"Sub Category"}),
Append = Table.Combine({FillDown, OnlyTotals})
in
Append
If you need it to get back to the starting order, add an index column before doing any filtering and then sort by that index as your last step.
Try this
#"Duplicated Column" = Table.DuplicateColumn(#"PreviousStep", "Subcategory", "Dupe"),
#"Filled Down" = Table.FillDown(#"Duplicated Column",{"Dupe"}),
#"Added Custom" = Table.AddColumn(#"Filled Down", "Custom", each if [Subcategory]=null then (if Text.Contains([Dupe], "Total") then [Subcategory] else [Dupe]) else [Dupe]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Subcategory", "Dupe"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Subcategory"}})

Adding different row values for Birt

I got this selection of data from my sql:
I would like to add Cancelled, Disputed and Resolved together and then divide the result with the total shipped. All of this should be done with an Expression.
So x / 303 where x is the sum of the desired values.
Goal would be to get a % where I can tell how good my shipping is.
I would then like to display the result in a text label next to a graph.
How do I do that?
You should use computed columns in your data set:
Add a SUM on the column Total and a filter only matching the rows based on the column Status you want to select. The expression should look like:
if (row["Status"] == "Cancelled" || row["Status"] == "Disputed"
|| row["Status"] == "Resolved")
true
else
false
create a second computed column only containing the "Total" value where the Status is Shipped.
if (row["Status"] == "Shipped")
row["Total"]
Then create a third computed column where you divide both computed values and you are done.
row["sum"] / row["shipped"]
create a new parameter and refer the image
create new static values and allow multiple values to be selected.
So, accordingly edit your SQL queries

SQL Statement to delete only one row out of duplicates

So I am working in Ruby, and say I have 6 rows in a table of two columns that are exactly identical. In my case, my table "campaign_items" has two columns "campaign_name" and "item." I would like to delete only one row out of the 6 duplicates using a single query. I started with this:
db.exec("DELETE FROM products WHERE campaign_name = '#{camp_name}' AND product_type = 'fleecejacket' AND size = '#{size_array[index]}'")
Which of course deleted all items of that condition. So I found in another question an answer along these lines:
db.exec("DELETE FROM products a WHERE a.ctid <> (SELECT min(b.ctid) FROM products b WHERE a.key = b.key)")
However, this would delete all duplicates except for one. I have not found a way that only deletes a SINGLE row that has duplicates. Is there a delete top query that I am looking for? Thanks in advance.
Edit: I also have a column "id" which is a primary key.
So I definitely overthought this, but all that is needed is this:
x = db.exec("SELECT * FROM campaign_items WHERE campaign_name = '#{camp_name}' AND item = 'fleecejacket'")
id = x[0]['id']
db.exec("DELETE FROM campaign_items WHERE campaign_name = '#{camp_name}' AND item = 'fleecejacket' AND id = '#{id}'")
Get the unique id from the first duplicate (since it doesn't matter which one is deleted) and delete the row with that id.

join two tables in linq with special conditions

I hope one can help me, I am new in linq,
I have 2 tables name tblcart and tblorderdetail:
I just show some fields in these two tables to show whats my problem:
tblCart:
ID,
CartID,
Barcode,
and tblOrderDetail:
ID,
CartID,
IsCompleted
Barcode
when someone save an order, before he confirms his request,one row temporarily enter into the tblCart,
then if he or she confirms his request another row will be inserted into the tblOrderDetail ,
Now I wanna not to show the rows that is inserted into tblOrderDetailed(showing just temporarily rows which there is in tblCart),
In another words, if there is rows in tblCart with cartID=1 and at the same time there is the same row with CartID= 1 in tblOrderDetail, then I dont want that Row.
All in all, Just the rows that there isnt in tblOrderDetail, and the field to realize this is CartID,
I should mention that I make Iscompleted=true, and with that either we can exclude the rows we do not want,
I did this:
var cartItems = context.tblCarts
.Join(context.tblSiteOrderDetails,
w => w.CartID,
orderDetail => orderDetail.cartID,
(w,orderDetail) => new{w,orderDetail})
.Where(a=>a.orderDetail.cartID !=a.w.CartID)
.ToList()
however it doesn't work.
one example:
tblCart:
ID=1
CartID=1213
Barcode=4567
ID=2
CartID=1214
Barcode=4567
ID=3
CartID=1215
Barcode=6576
tblOrderDetail:
ID=2
CartID=1213
Barcode=4567
IsCompleted=true
with these data it should just show the last two Row in tblCart, I mean
ID=2
CartID=1214
Barcode=4567
ID=3
CartID=1215
Barcode=6576
This sounds like a case for WHERE NOT EXISTS in sql.
roughly translated this should be something like this in LINQ:
var cartItems = context.tblCarts.Where(crt => !context.tblSiteOrderDetails.Any(od => od.CartID == crt.cartID));
If you have a navigation property on cart to reference details (I'll assume it's called Details), then:
var results=context.tblCarts.Where(c=>!c.Details.Any(d=>d.IsCompleted));

Resources