How to get active rent's details for given mon

2019-09-20 14:21发布

问题:

I want to show active rent's details for given month in power bi. I have a table where i have rent details. In that table i have start date, end date and how many months. So when i select a month in power bi visual i want to show what are the rents currently active using power bi

Example

House A 1.1.2019 31.12.2019 12 House B 1.2.2019 31.05.2019 03 House C 1.4.2019 31.12.2019 08

If i select month June result should be House A and House C, and if i select march result should be All three (A, B, C)

Can someone help me with power bi?

Please find my solution here - (https://app.powerbi.com/groups/me/reports/b12bf4b6-14f1-41e1-9bf9-832509dce3e9?ctid=8ac0e5ca-0835-4daf-af23-c940683adbef)

I get table using above solution. I need to match subscription_id with other tables and get data but currently it not allowing. Any solution

回答1:

Lets assume you have the data in one table called 'Rentals'

The Rentals table contains the following info (but not limited to):

[Location] | [Rent_Start] | [Rent_End]

Step one:

Create a Calendar table:

SELECTCOLUMNS(
    GENERATESERIES(
        MIN(Rentals[Rent_Start]);
        MAX(Rentals[Rent_End])
    );
    "Date"; [Value]
)

Add two calculated columns to your calendar table [Month Name] and [Month Number]

Step 2: Crossjoin the rental table with the calendar in a new table, add a flag column which indicates if the location is in use or not. Filter rows when location is not in use and the select the columns you actually need:

RunningTime = 
SELECTCOLUMNS(
    FILTER(
        ADDCOLUMNS(
            CROSSJOIN(Rentals; Calendar);
            "In_Use";
            IF([Date] >= [Rent_Start] && [Date] <= [Rent_End]; 1; blank() )
        );
        [In_Use] = 1
    );
    "Location"; [Location];
    "Start"; [Rent_Start];
    "End"; [Rent_End];
    "Date"; [Date]
)

Step 3: Create a 1:* relationship between Calendar[Date] and the RunningTime[Date] columns. Now you can create a slicer from the Calendar using Month name and in a visual table with RunningTime[Location] you can no see which locations are in use when selecting certain month.

(Month number is needed to sort [Month Name] in "correct" order and not alphabetically)

Edit 1: Let's see, I think my code does what you need, as far as I can understand you. If the following clarification does not work for you then get back to me by uploading your model (or example thereof). Also, not sure what you mean by differing between houses in use and total occupancy.

As far as I can tell from your image and text my [Rent_Start], and [Rent_End] corresponds to your columns [start_date] and [end_date], both in datetime format. So I don't see what the problem is?

If you just want to know how many houses are in use then you need this measure:

Houses in use = 
DISTINCTCOUNT('RunningTime'[Location])

These three columns are needed in my solution, though you can have more.

Then I end up with a new table looking like this:

This new table is linked to the Calender like this:

Then in the visual table I use [Location], [Start], and [End] columns from the "RunningTime" table. And I create a slicer based on Month_Name from my Calendar table, like this (note: no month has been selected in this picture):

If I now select January, I get the following:

If I select March, I get the following: