Commit 490b7680 authored by Powell, Eric's avatar Powell, Eric
Browse files

Insitial commit of mquery / DAX to power a Gantt chart in PowerBI

parent a564a23b
Loading
Loading
Loading
Loading
+102 −0
Original line number Diff line number Diff line
-- estimated_duration

let
    Source = PostgreSQL.Database("hgis-prj-mgmt:5438", "proj_status"),
    f_o_isg_vext_task_duration = Source{[Schema="f_o_isg",Item="vext_task_duration"]}[Data],
    JoinedTables = Table.NestedJoin(f_o_isg_vext_task_duration, {"title"}, NumericalData, {"Title"}, "title_matches", JoinKind.Inner),
       ExpandedTable = Table.ExpandTableColumn(JoinedTables, "title_matches", {"effort_wt"}, {"NumericalData.effort_wt"}),
     weighted_fte = Table.AddColumn(ExpandedTable, "weighted_fte", each [estimated_fte_months] * [NumericalData.effort_wt]),
    wt_dur_day = Table.AddColumn(weighted_fte, "weighted_duration_days", each [def_task_dur_days] *  [NumericalData.effort_wt]),
    wt_dur_mo = Table.AddColumn(wt_dur_day, "weighted_duration_months", each [def_task_dur_mo] *  [NumericalData.effort_wt]),
    #"Changed Type" = Table.TransformColumnTypes(wt_dur_mo,{{"weighted_duration_days", type number}, {"weighted_duration_months", type number}, {"weighted_fte", type number}})
       
in
    #"Changed Type"

-- estimatedcost

let
    Source = PostgreSQL.Database("hgis-prj-mgmt:5438", "proj_status"),
    f_o_isg_v_task_cost_estimate = Source{[Schema="f_o_isg",Item="vext_task_cost_estimate"]}[Data],
     JoinedTables = Table.NestedJoin(f_o_isg_v_task_cost_estimate, {"title"}, NumericalData, {"Title"}, "title_matches", JoinKind.Inner),
       ExpandedTable = Table.ExpandTableColumn(JoinedTables, "title_matches", {"effort_wt"}, {"NumericalData.effort_wt"}),
    weighted_cost = Table.AddColumn(ExpandedTable, "weighted_cost", each [estimated_cost] * [NumericalData.effort_wt]),
  #"Changed Type" = Table.TransformColumnTypes(weighted_cost,{"weighted_cost", type number})
       
in
    #"Changed Type"

-- projectscheduie

ProjectSchedule = 
VAR StartDate = TODAY() // A fixed start date for all parallel schedules

// This variable first joins the necessary data from all three tables.
// It iterates over the central 'metadata' table and uses RELATED()
// to pull in columns from the related 'v_ian_duration' and 'FeaturePrioritization' tables.
VAR TasksJoined =
    ADDCOLUMNS(
        'metadata',
        // "Platform", 'metadata'[Platform],
        "Duration", RELATED('estimated duration'[weighted_duration_months]),
        "Rank", RELATED('NumericalData'[mgt_weighted_rank])
    )

VAR TasksSorted =
    ADDCOLUMNS(
        TasksJoined,
        // A hidden column for stable sorting using the Rank
        "__SortValue", [Rank]
    )

// Step 1: Create a table with the TaskStartDate column
VAR TasksWithStartDates =
    ADDCOLUMNS(
        TasksSorted,
        // Calculate the cumulative duration of all preceding tasks
        // using SUMX and EARLIER. This is the core logic for sequencing.
        "TaskStartDate",
            VAR CumulativeDuration =
                SUMX(
                    // The FILTER has been modified to only sum durations for tasks
                    // that share the same 'Primary Resource'
                    FILTER(
                        TasksSorted,
                        // Group the calculation by Primary Resource
                        [Primary Resource] = EARLIER([Primary Resource]) &&
                        // Then, apply the sorting logic for the cumulative sum
                        (
                            [__SortValue] < EARLIER([__SortValue]) || (
                                [__SortValue] = EARLIER([__SortValue]) &&
                                [title] < EARLIER([title])
                            )
                        )
                    ),
                    [Duration]
                )
            RETURN
                // EDATE is used here to correctly add the cumulative months to the start date.
                EDATE(StartDate, CumulativeDuration)
    )

// Step 2: Create a new table that now has access to the TaskStartDate column
VAR TasksWithBothDates =
    ADDCOLUMNS(
        TasksWithStartDates,
        "TaskEndDate",
            // The end date is calculated by adding the task's duration in months to its start date.
            EDATE([TaskStartDate], [Duration])
    )

// Return the final table, selecting only the necessary columns
RETURN
    SELECTCOLUMNS(
        TasksWithBothDates,
        "Title", [title],
        "Primary Resource", [Primary Resource],
        "Rank", [Rank],
        "Duration", [Duration],
        "StartDate", [TaskStartDate],
        "EndDate", [TaskEndDate]
    )