Keep first occurrence of value and set subsequent values to 0
I'm working with invoice data that has multiple line items for each invoice. The issue is that the tax field has the entire invoice tax value on each line item as shown below:
| Invoice | Item | Amount | Tax |
|---------|------|--------|-----|
| 1 | A | 10 | 6 |
| 1 | B | 20 | 6 |
| 1 | C | 30 | 6 |
What I would want
| Invoice | Item | Amount | Tax |
|---------|------|--------|-----|
| 1 | A | 10 | 6 |
| 1 | B | 20 | 0 |
| 1 | C | 30 | 0 |
So essentially I want to keep the tax value on the row with the first occurrence of the invoice number and then every occurrence of that invoice number after I want to set the tax value to 0. Is this even possible in ETL or SQL.
Comments
-
Hi @Crisocir
You can do this utilizing a window function (If you don't have it in your instance you'd need to talk to your CSM to get it enabled).
Essentially this beast mode is calculating the row numbers (within each invoice) and then checking to see if it's the first row, if so return the tax amount otherwise return 0.
CASE WHEN SUM(SUM(1)) OVER (PARTITION BY `Invoice` ORDER BY `Item`) = 1 THEN `Tax` ELSE 0 END
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**0 -
I would restructure this data in ETL.
If Tax is the same across all invoice lines, take your Invoice Lines and Remove Tax. then add a column with a fixed value of "Invoice Lines.
Then UNION a copy of the data with one row per Invoice for Tax. you can put amount into the same column or have a separate column for tax amount. add a fixed column with the value 'Tax Lines'
You can run with the Window'ed' function hack, but it's dependent on you showing each invoice line in your visualizations which may not be suitable for higher level consolidations.
Jae Wilson
Check out my 🎥 Domo Training YouTube Channel 👨💻
**Say "Thanks" by clicking the ❤️ in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 616 Datasets
- 2.2K Magic ETL
- 3.8K Visualize
- 2.5K Charting
- 738 Beast Mode
- 56 App Studio
- 40 Variables
- 684 Automate
- 176 Apps
- 452 APIs & Domo Developer
- 46 Workflows
- 10 DomoAI
- 35 Predict
- 14 Jupyter Workspaces
- 21 R & Python Tiles
- 394 Distribute
- 113 Domo Everywhere
- 275 Scheduled Reports
- 6 Software Integrations
- 123 Manage
- 120 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 108 Community Announcements
- 4.8K Archive