DAX/PowerQuery根据限制检索与一个国家的最后日期

发布于 2025-01-31 16:51:15 字数 1299 浏览 1 评论 0 原文

我有一个包含: 在各个国家 /地区出售各种水果的帐户,即我正在处理一些指标,我真的想计算一个国家在一个国家的第一批水果与同一国家的最后出售水果之间的时间之间的时间。

例如:“两天之间”在奥地利首次出售了橙色,最后出售了奥地利的橙色,

我已经使用措施解决了这一点,但这不允许我进一步工作,因为这会给我带来循环依赖的错误(所有人)参考!)

我想知道我是否可以这样做不那么复杂?一个想法是在PowerQuery中设置额外的表格,该桌子是由Fruit和Country出售的最新日期,类似于Excel中的Pivot可以做的事情,此后,我可以链接到我的主桌子并使用相关选项来检索日期。

有什么想法吗?

PowerBi中的当前序列:

DURATION = 
VAR dispdate =
    MIN( 'Test dat'[Dispath] )
VAR lastsoldthiscountry =
    CALCULATE(
        MAX( 'Test dat'[Last sold date] ),
        ALL( 'Test dat' ),
        SUMMARIZE( 'Test dat', 'Test dat'[Fruit], 'Test dat'[Country] )
    )
RETURN
    IF(
        NOT ( ISBLANK( lastsoldthiscountry ) ) && NOT ( ISBLANK(dispdate) ),
        INT( lastsoldthiscountry - dispdate )
    )

Excel原始测试:

PBI文件:PBI文件: https://www.dropbox.com/s/ljnyics.com/s/ljnyics6d7n744oc/test。 pbix?dl = 0

I have a data set containing:
Accounts selling various fruits in various countries, im working on some metrics and I really want to calculate the time between the first sold fruit in a country to the last sold fruit in the same country.

For instance: "days between" First sold ORANGE in Austria to Last sold ORANGE in AUSTRIA

I have solved this using a measure, which works but this doesn't allow me to work further with it as it throws me error with circular dependicies (ALL reference!)

I wonder if I can do this less complicated? an idea would be to set up an additional table in powerquery having latest date sold by fruit and country similar to what a pivot can do in excel, after that I could link to my main table and retrieve the date by using the RELATED option.

Any ideas?

Current muesure in powerBI:

DURATION = 
VAR dispdate =
    MIN( 'Test dat'[Dispath] )
VAR lastsoldthiscountry =
    CALCULATE(
        MAX( 'Test dat'[Last sold date] ),
        ALL( 'Test dat' ),
        SUMMARIZE( 'Test dat', 'Test dat'[Fruit], 'Test dat'[Country] )
    )
RETURN
    IF(
        NOT ( ISBLANK( lastsoldthiscountry ) ) && NOT ( ISBLANK(dispdate) ),
        INT( lastsoldthiscountry - dispdate )
    )

Excel raw test:
https://www.dropbox.com/scl/fi/cppyzagm4ahusrxmadlg6/Test_data_withPivot.xlsx?dl=0&rlkey=2nf5wzl7etwr2hqg2lh4oyifx

PBI file:
https://www.dropbox.com/s/ljnyics6d7n74oc/test.pbix?dl=0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2025-02-07 16:51:15

如果我正确理解您,则可以在电源查询中完全做您想做的事情。

  • 组成 fruit country
  • 开始开始日期 max 代码>结束日期
  • 添加列以计算天数差异:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Fruit", type text}, {"Account ID", type text}, {"Country", type text}, 
        {"Sold amount", Int64.Type}, {"Dispath", type date}, {"First sold date", type date}, {"Last sold date", type date}}),
    
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Fruit", "Country"}, {
        {"First Date Sold", each List.Min([First sold date]), type nullable date}, 
        {"Last Date Sold", each List.Max([Last sold date]), type nullable date}}),
    
    #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Fruit", Order.Ascending}, {"Country", Order.Ascending}}),
    
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Days Sold", each 
        Duration.Days([Last Date Sold]-[First Date Sold]),Int64.Type)
in
    #"Added Custom"

”在此处输入图像描述”

法国苹果的负数似乎是由于数据输入错误

如果缺少日期条目,则表将显示一个零,因为PQ如何处理算术操作中的null

If I understand you correctly, you can do what you want entirely in Power Query.

  • Group by Fruit and Country
  • Aggregate by Min of Start Date and Max of End Date
  • Add column to calculate the Days difference:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Fruit", type text}, {"Account ID", type text}, {"Country", type text}, 
        {"Sold amount", Int64.Type}, {"Dispath", type date}, {"First sold date", type date}, {"Last sold date", type date}}),
    
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Fruit", "Country"}, {
        {"First Date Sold", each List.Min([First sold date]), type nullable date}, 
        {"Last Date Sold", each List.Max([Last sold date]), type nullable date}}),
    
    #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Fruit", Order.Ascending}, {"Country", Order.Ascending}}),
    
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Days Sold", each 
        Duration.Days([Last Date Sold]-[First Date Sold]),Int64.Type)
in
    #"Added Custom"

enter image description here

The negative number for France Apples seems due to a data entry error

If there is a missing date entry, the table will show a null, due to how PQ processes nulls in arithmetical operations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文