If you had wondered out loud five years ago whether Microsoft (MSFT 0.61%) could be the first public company to reach a $1 trillion valuation, you would have been laughed out of the room. The software giant was worth less than $250 billion back then, hobbled by the lackluster performance of Windows 8, its losing battle in the smartphone market, and fears that cloud computing would render its lucrative software business obsolete.

Fast-forward to today, and you wouldn't be so crazy to think that Microsoft could power ahead of the competition to a 12-figure valuation. CEO Satya Nadella, who took over in 2014, made cloud computing a priority. He moved quickly to grow the Azure cloud platform and shift to subscription software. The result has been an explosion in the stock price. Microsoft is now valued at around $650 billion.

Microsoft stock would need to gain another 50% or so to reach the trillion-dollar mark. That's a big move, but it's not unthinkable.

Microsoft CEO Satya Nadella on a stage giving a talk.

Image source: Microsoft.

Not cheap but not all that expensive, either

Microsoft generated $32.3 billion of free cash flow and $22.1 billion of net income over the past 12 months. At the current stock price, the price-to-free cash flow ratio is about 20, and the price-to-earnings ratio is about 29. Given that the S&P 500 currently trades for roughly 25 times trailing-12-month earnings, these numbers don't seem out of line.

Reaching a $1 trillion valuation would require these valuation metrics to increase quite a bit, all else being equal. The P/FCF ratio would rise to 31, and the P/E ratio would jump to 45. Microsoft hasn't traded for 45 times earnings since the early 2000s, in the aftermath of the dot-com crash.

MSFT PE Ratio (TTM) Chart

MSFT P/E ratio (TTM) data by YCharts.

Reaching that level again isn't impossible, especially if the market continues to grind higher. Remember that Amazon, worth almost as much as Microsoft, trades for nearly 300 times TTM earnings. But hitting the trillion-dollar mark will probably require some earnings growth over the next few years.

The cloud growth engine

Analysts are Evercore ISI believe that Microsoft could hit a $1 trillion valuation by 2020 or sooner, driven by the company's cloud and subscription businesses. The analysts see Office 365 commercial, the subscription version of the Office suite aimed at business users, generating nearly $27 billion of revenue by fiscal 2021. The consumer side of Office 365 is seen producing another $5 billion of revenue.

The Evercore analysts are particularly optimistic about Azure, Microsoft's cloud computing platform. Azure is the No. 2 player in the public infrastructure-as-a-service market behind Amazon Web Services, and it's still nearly doubling revenue year over year. The analysts expect Azure to be one of the main earnings and free cash flow drivers for Microsoft over the next decade, potentially generating operating margins in excess of 30%. That's even higher than the margins currently managed by Amazon Web Services.

With the public IaaS market growing at a double-digit rate, Microsoft's cloud business could get a lot bigger in the coming years. And growth could be even faster if Microsoft manages to steal market share away from Amazon. If Microsoft's cloud margins are close to what Evercore is predicting, a $1 trillion market capitalization isn't very far-fetched at all.

One thing that could derail Microsoft

If there's anything that could stop Microsoft from reaching $1 trillion, it's the commodification of the cloud infrastructure market. Here's the problem: The building blocks of cloud infrastructure, like virtual machines and storage, are pretty much the same no matter the provider. A VM on AWS is, for the most part, the same as a VM on Azure. In a market where price is the main differentiating factor, expecting lofty margins once the market matures probably isn't realistic.

There are higher-value products offered through Azure, like databases and machine learning services, that will almost certainly provide better margins. But I'm not sure a 30% operating margin overall for Azure in the long run is even in the ballpark of realistic. In the short run, with the market still growing so fast, sure. But once things settle down, I'm not convinced.

Could Microsoft beat out Apple and Amazon in the race to $1 trillion? It's certainly possible. Earnings could soar over the next few years as the cloud and subscription businesses grow. Add in a bit of multiple expansion, and you have the recipe for a $1 trillion valuation.

But if earnings growth disappoints, perhaps due to renewed price wars in the cloud computing market, the $1 trillion mark for Microsoft may end up being many years away.