DAX & Power Query – 如果销售退货,删除交易行

huangapple go评论74阅读模式
英文:

DAX & Power Query - Removing transaction line if sale is returned

问题

首次发帖,先行致谢!
我有一个作为数据源的简单销售表。
在这个表中,我有所有的维度和事实,数据模型没有组织成星型模式。
我认为这个问题不需要数据模型进行最佳构建。

其中一列跟踪销售数量,并且如果商品退回,则显示为-1。

在Power Query中,
我希望对所有退货的情况进行处理,即除去值为-1的行,但也除去与+1销售相关的行。

我不希望仅仅按照发票号进行筛选并删除,因为在同一张发票中可能还有其他未退货的商品。

我希望在我的最终表中只包含最终记录。

谢谢

到目前为止,我尚未找到在Power Query中解决这个问题的方法。


编辑,提供更多上下文
数据集摘要:
我想保留绿色的行并移除黄色的行。
一个发票包含三行,两行是卖出和退货的商品,还有一行是正常购买的商品。后者需要保留在记录中。

[图片:数据库中相关列的Excel版本]

https://i.stack.imgur.com/CiiiC.png

英文:

First time posting, thanks in advance !
I have a simple sales table acting as a data source.
In this table I have all my dimensions and facts, the data model is not organized as a star schema.
I believe this question does not require the data model to be optimally built.

One column tracks the Sales Quantity and shows -1 if the item is returned.

In Power Query.
I wish, for all returns, to eliminate the line with the -1, but also the associated line with the +1 sale.

I do not wish to simply filter by the Invoice Number and remove it, as in the same invoice I may have other items who were not returned.

I wish to have in my final table only final records.

Thank you

Did not manage to identify a way to tackle the issue in Power Query as of yet.


Edit, addition of further context
Dataset abstract:
I would like to keep the green row and remove the yellow ones.
One invoice features three lines, two for one item being sold and returned, and one for an item being purchased for good. That latter one is to stay in the records.

[IMAGE : Excel version of relevant column in data base ]

https://i.stack.imgur.com/CiiiC.png

答案1

得分: 0

因为我不知道你的数据的确切性质,所以我将为你提供一个关于你的问题的一般解决方案,这将对你有很大帮助。

首先,我将把你的数据转化为事实和问题。然后,根据我的经验分析许多不同的数据集,我将回答这些问题;最后但同样重要的是,我将为你提供解决方案。

Facts,

  1. [销售金额包括税] 列的值为负数,因为Excel使用括号包括负数,正如你的数据所示。

  2. 原始销售记录和退货销售记录必须具有这些列的相同值,我们将使用它们来识别原始销售记录:

    • [销售金额包括税] 列的绝对值。
    • [发票号]、[部门]、[子部门]、[子部门分组]、[商品代码] 列。

    这一点至关重要,因为我们将使用这些列来识别原始销售记录。

Questions,

  1. 商品代码列的值是否与原始销售行和退货销售行相似?

    我的回答: 应该相似,因为你正在退还描述在商品代码列中的同一商品,但我担心你的数据显示前两行的商品代码不相同,这是错误吗?

  2. 销售数量列是否总是填写为-1,即使原始销售数量例如是三双鞋?

    我的回答: 销售数量列应该描述某人购买的物品数量,所以如果我最初购买了2件东西,但由于某种原因不喜欢它们,我将退还两件物品,因此该列的值将是-2。

如果我的上述假设是正确的,你需要在Power BI查询中执行以下操作:

  1. 将销售表导入到Power BI中。
  2. 当你导入数据并在Power Query Editor中查看数据时,你会发现[销售金额包括税] 列的值显示为负数而不是括号,就像你在图像中看到的那样。
  3. 创建销售表的副本并将其重命名为"退货销售",然后筛选此表以仅包括[销售金额包括税] 小于0的行。
  4. 筛选销售表以仅包括[销售金额包括税] 大于或等于0的行。

然后,在"退货销售"表中:

  1. 添加一个名为"ReturnedSaleFlag"的自定义列,并将其值设置为1。
  2. 更改"ReturnedSaleFlag"列的数据类型为整数。
  3. 将[销售金额包括税] 转化为绝对值。

然后返回销售表,并将其与"退货销售"表合并,使用多个列,这些列是事实部分第二点中列出的列。还请查看描述如何基于多列合并查询的链接。

扩展新的"退货销售"列并仅选择"ReturnedSaleFlag"列,并确保取消选中"使用原始列名作为前缀"的选项。

最后,你的辛勤工作将在销售表中创建一个标志,让你知道哪些销售记录已经退货。

现在,你可以筛选销售表,仅保留新的"ReturnedSaleFlag"列中为null值的记录,然后删除此列。

最后保存并应用,并分析你的销售和退货销售中的额外信息。

希望我以某种方式帮助了你;如果是这样,请标记这个回答并投票支持它。

英文:

Because I do not know the exact nature of your data, I will give you a general solution for your problem, which will help you immensely.

First, I will transform your data into facts and questions. Then I will answer the questions depending on my experience analyzing many different datasets; last but not least, I will provide you with the solution.


Facts,

  1. [Sales Amount Include Tax] column values for Return Sales are negative numbers because excel format negative numbers by including them with parentheses, as your data shows.

  2. The Original Sale record and the Return Sale record must have the same values for these columns, which we will use to know the Original Sales record:

> The absolute value for the [Sales Amount Include Tax] column.

> [Invoice No], [Department], [Sub Dept], [Sub Dept GROUP], [Item Code] columns.

>>> This point is critical because we will use these columns to know the Original Sales record.

Questions,

  1. Is the Item code column value similar to the original sale row and the returned sales row?

    My answer: It should be because you are returning the same item that the Item code column describes, but I am concerned that your data shows that the first two rows don't have the same Item code. Is it by mistake?

  2. Is the Sales Quantity column always filled with -1, even if the original sales quantity was, for example, three pairs of shoes?

    My answer: The Sales Quantity column should describe the number of items someone bought, so if I originally bought 2 things but didn't like them for a reason, I will return two items so that the column value will be -2.


If my above assumptions are correct, you need to do the following in the Power BI query:

1. Import your Sales table to Power BI. See this image that shows the test data I used.
DAX & Power Query – 如果销售退货,删除交易行

2. When you import your data and see it in the Power Query Editor, you will see that the Sales Amount Include Tax column value shows minus rather than parentheses for Return Sales Rows, as you can see in this image.
DAX & Power Query – 如果销售退货,删除交易行

3. Create a copy of your Sales table and Rename it to Returned Sales, then filter this table to include only [Sales Amount Include Tax] that are less than 0, as you can see in this image.
DAX & Power Query – 如果销售退货,删除交易行

4. Filter the Sales table to include only [Sales Amount Include Tax] greater than or equal to 0, as you can see in this image.
DAX & Power Query – 如果销售退货,删除交易行

> P.S. Now comes the fun part DAX & Power Query – 如果销售退货,删除交易行

1. In Returned Sales table, add a custom column name it ReturnedSaleFlag, then give it a value of 1, as you can see in this image.
DAX & Power Query – 如果销售退货,删除交易行

2. Change ReturnedSaleFlag column type to Whole number.

3. In Returned Sales table, transform [Sales Amount Include Tax] to an Absolute Value, as you can see in this image.
DAX & Power Query – 如果销售退货,删除交易行

4. Now you need to return to the Sales table and merge it with Returned Sales table using multiple columns which are the column listed in the second point of the fact section, as the image shows. Also, see this link that describes how to Merge query based on multi columns.
DAX & Power Query – 如果销售退货,删除交易行

5. Expanded the new Returned Sales column in Sales table and only select ReturnedSaleFlag column and make sure to remove the check from use original column name as a prefix, as the image shows.
DAX & Power Query – 如果销售退货,删除交易行

6. Finally, all your previous hard work created a flag in your Sales table that will let you know the Sales record that had been returned. DAX & Power Query – 如果销售退货,删除交易行

7. Now you filter the Sales table to keep the records that only have null values for the new ReturnedSaleFlag column then remove this column, as the image shows.
DAX & Power Query – 如果销售退货,删除交易行

8. In the end save and apply and analyze your Sales and your extra info in the Returned sales.


I hope I helped in a way; if so, please mark this as an answer and vote for it DAX & Power Query – 如果销售退货,删除交易行

huangapple
  • 本文由 发表于 2023年2月16日 14:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定