Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
Ibad_Khan
Helper II
Helper II

Sales of Previous month if a value is found

Hello PBI Community, hope you all're doing good.

 

I want to cover a scenario for which I've pasted a screen shot below.

 

Ibad_Khan_0-1669185323454.png

 

Scenario : We want to search and move forward from month Jan to Dec, and if a tag is found in any month, we want to have a sales of that month in all the months after that months. 

For eloboration values needed are highlighted green.

 

Looking forward for some positive and quick response as usual.

 

@amitchandak  @v-yadongf-msft  @v-janeyg-msft @v-kkf-msft  @Jihwan_Kim 

 

Regards,

Ibad Khan.

1 ACCEPTED SOLUTION

Hi , @Ibad_Khan 

Thank you for your quick reponse, i create the test data as you provided.

vyueyunzhmsft_1-1669270096516.png

 

Here are the steps you can refer to :
(1)We need to unpivot two tables in Power Query Editor to get the table we need:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc89DoMwDAXgu2RmwM6P02tUnYoYOkQVA3D/rTF9eRJDnDd8dpxlCa/22SVM4X0eze9n+27n4Umyl9qLetCwTn+u4EruaR4tevVF8ggeyT2JoUflzhN4Ik9cxnvk8ok+w2f6PCbPCEZcgAtxGZ/k/v2BwQ3cyI0bCxcq9BW+0nuSB8b76Xj9AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Team = _t, Zone = _t, Region = _t, #"22-Jan" = _t, #"22-Feb" = _t, #"22-Mar" = _t, #"22-Apr" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Team", type text}, {"Zone", type text}, {"Region", type text}, {"22-Jan", Int64.Type}, {"22-Feb", Int64.Type}, {"22-Mar", Int64.Type}, {"22-Apr", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Team", "Zone", "Region"}, "Attribute", "Value"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Columns",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Value", "Sales"}})
in
    #"Renamed Columns"
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc89CsAgDAbQuzi71P9zFKeKg4NIh+r9t1aMaSyFkGR4gS8hMJ/TtTHOjlZzn3suZ6t986k8fVTfIx9agBaoxYvGwaQSqEQqKYWDqRVohVphAOo0OI1O/zoDzqAzy0OEWqAWqf2mJNqBdqjdGiDe", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Team = _t, Zone = _t, Region = _t, #"22-Jan" = _t, #"22-Feb" = _t, #"22-Mar" = _t, #"22-Apr" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Team", type text}, {"Zone", type text}, {"Region", type text}, {"22-Jan", type text}, {"22-Feb", type text}, {"22-Mar", type text}, {"22-Apr", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Team", "Zone", "Region"}, "Attribute", "Value"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Columns",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Value", "Tag"}})
in
    #"Renamed Columns"

The table will be transformed like this:

vyueyunzhmsft_2-1669270155658.png

And now we need to apply to the desktop and we do not need to create a relationship between two tables:

vyueyunzhmsft_3-1669270189715.png

(2)We need to create two measures like this:

Tag = var _region = MAX('Sales Data'[Region])
var _zone = MAX('Sales Data'[Zone])
var _team =MAX('Sales Data'[Team])
var _date = MAX('Sales Data'[Attribute])
var _tag = FILTER('Tag Data','Tag Data'[Team]= _team &&'Tag Data'[Zone]=_zone &&'Tag Data'[Region]=_region && 'Tag Data'[Attribute]=_date)
return
MAXX(_tag,[Tag])
Previous = 
var _current_date = MAX('Sales Data'[Attribute])
var _team = MAX('Sales Data'[Team])
var _region =MAX('Sales Data'[Region])
var _zone = MAX('Sales Data'[Zone])
var _previos_tag = MAXX( FILTER('Tag Data' , 'Tag Data'[Team]=_team && 'Tag Data'[Region]=_region && 'Tag Data'[Zone]=_zone && 'Tag Data'[Attribute]<_current_date && 'Tag Data'[Tag]<> BLANK()) , [Attribute])
var _previous_value = FILTER(ALLSELECTED('Sales Data') , 'Sales Data'[Team] =_team && 'Sales Data'[Region]=_region && 'Sales Data'[Zone]=_zone && 'Sales Data'[Attribute]=_previos_tag)
return
SUMX(_previous_value,[Sales])

(3)Then we can put the fields we need on the Matrix measure and we will meet your need.

And for your need we need to configure the Matix parameter in the Format Pane:

vyueyunzhmsft_4-1669270349358.png

 

vyueyunzhmsft_5-1669270377185.png

In the end we can meet your need:

vyueyunzhmsft_6-1669270402878.png

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

View solution in original post

5 REPLIES 5
v-yueyunzh-msft
Community Support
Community Support

Hi , @Ibad_Khan 

According to your description, you want to "Sales of Previous month if a value is found".

Here are the steps you can refer to :

(1)I do not have your original data , so i create  my test data like this:

vyueyunzhmsft_0-1669260467697.png

(2)We can create a measure :

Measure = var _current_date =MAX('Table'[Date])
var _current_team = MAX('Table'[Team])
var _current_tab = MAX('Table'[Tag])
var _max_date =MAXX( FILTER( ALLSELECTED('Table') , 'Table'[Team]=_current_team && 'Table'[Date]<_current_date && 'Table'[Tag]<> BLANK()) , [Date])
var _pre_sales = SUMX( FILTER(ALLSELECTED('Table'),'Table'[Date] = _max_date && 'Table'[Team]=_current_team) ,[Sales])
return
_pre_sales

(3)Then we can put the fields we need and the measure on the visual and we will meet your need :

vyueyunzhmsft_1-1669260508401.png

 

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hello @v-yueyunzh-msft Thanks alot for your valuable responce , we are very close to it. I'm really sorry that I forgot to mention that we want to look data by team as well as by some other dimensions like zone and regions too.

 

I'm sharing the dummy data with desired output with you in the form of screenshots, as unfortunately I'm unable to attach the excel sheet as well as Pbix file here.

 

Sales Data: 

Ibad_Khan_0-1669263249172.png

 

Tag Data:

Ibad_Khan_1-1669263281835.png

 

Desired Output:

Ibad_Khan_4-1669263501268.png

 

Looking forward some postive responce as usual.

 

Best Regards,

Ibad Khan.

 

 

 

 

 

Hi , @Ibad_Khan 

Thank you for your quick reponse, i create the test data as you provided.

vyueyunzhmsft_1-1669270096516.png

 

Here are the steps you can refer to :
(1)We need to unpivot two tables in Power Query Editor to get the table we need:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc89DoMwDAXgu2RmwM6P02tUnYoYOkQVA3D/rTF9eRJDnDd8dpxlCa/22SVM4X0eze9n+27n4Umyl9qLetCwTn+u4EruaR4tevVF8ggeyT2JoUflzhN4Ik9cxnvk8ok+w2f6PCbPCEZcgAtxGZ/k/v2BwQ3cyI0bCxcq9BW+0nuSB8b76Xj9AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Team = _t, Zone = _t, Region = _t, #"22-Jan" = _t, #"22-Feb" = _t, #"22-Mar" = _t, #"22-Apr" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Team", type text}, {"Zone", type text}, {"Region", type text}, {"22-Jan", Int64.Type}, {"22-Feb", Int64.Type}, {"22-Mar", Int64.Type}, {"22-Apr", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Team", "Zone", "Region"}, "Attribute", "Value"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Columns",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Value", "Sales"}})
in
    #"Renamed Columns"
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc89CsAgDAbQuzi71P9zFKeKg4NIh+r9t1aMaSyFkGR4gS8hMJ/TtTHOjlZzn3suZ6t986k8fVTfIx9agBaoxYvGwaQSqEQqKYWDqRVohVphAOo0OI1O/zoDzqAzy0OEWqAWqf2mJNqBdqjdGiDe", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Team = _t, Zone = _t, Region = _t, #"22-Jan" = _t, #"22-Feb" = _t, #"22-Mar" = _t, #"22-Apr" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Team", type text}, {"Zone", type text}, {"Region", type text}, {"22-Jan", type text}, {"22-Feb", type text}, {"22-Mar", type text}, {"22-Apr", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Team", "Zone", "Region"}, "Attribute", "Value"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Columns",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Value", "Tag"}})
in
    #"Renamed Columns"

The table will be transformed like this:

vyueyunzhmsft_2-1669270155658.png

And now we need to apply to the desktop and we do not need to create a relationship between two tables:

vyueyunzhmsft_3-1669270189715.png

(2)We need to create two measures like this:

Tag = var _region = MAX('Sales Data'[Region])
var _zone = MAX('Sales Data'[Zone])
var _team =MAX('Sales Data'[Team])
var _date = MAX('Sales Data'[Attribute])
var _tag = FILTER('Tag Data','Tag Data'[Team]= _team &&'Tag Data'[Zone]=_zone &&'Tag Data'[Region]=_region && 'Tag Data'[Attribute]=_date)
return
MAXX(_tag,[Tag])
Previous = 
var _current_date = MAX('Sales Data'[Attribute])
var _team = MAX('Sales Data'[Team])
var _region =MAX('Sales Data'[Region])
var _zone = MAX('Sales Data'[Zone])
var _previos_tag = MAXX( FILTER('Tag Data' , 'Tag Data'[Team]=_team && 'Tag Data'[Region]=_region && 'Tag Data'[Zone]=_zone && 'Tag Data'[Attribute]<_current_date && 'Tag Data'[Tag]<> BLANK()) , [Attribute])
var _previous_value = FILTER(ALLSELECTED('Sales Data') , 'Sales Data'[Team] =_team && 'Sales Data'[Region]=_region && 'Sales Data'[Zone]=_zone && 'Sales Data'[Attribute]=_previos_tag)
return
SUMX(_previous_value,[Sales])

(3)Then we can put the fields we need on the Matrix measure and we will meet your need.

And for your need we need to configure the Matix parameter in the Format Pane:

vyueyunzhmsft_4-1669270349358.png

 

vyueyunzhmsft_5-1669270377185.png

In the end we can meet your need:

vyueyunzhmsft_6-1669270402878.png

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

amitchandak
Super User
Super User

@Ibad_Khan ,

Do you have date? How you created previous month value

 

example using date table and time intelligence

 

MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))
MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))

 

 

Time Intelligence, DATESMTD, DATESQTD, DATESYTD, Week On Week, Week Till Date, Custom Period on Period,
Custom Period till date: https://youtu.be/aU2aKbnHuWs&t=145s

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak  I think this will not work in my scenario. I'm looking for something else.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.